I have managed to get the voice recognition component of android to send commands into jme.
[java]package jme3.innovationtech.co.uk;
import java.util.ArrayList;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.Toast;
import com.jme3.app.AndroidHarness;
import com.jme3.system.android.AndroidConfigChooser.ConfigType;
public class MainActivity extends AndroidHarness {
Game3 game;
private static final int REQUEST_CODE = 1234;
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onStop() {
super.onStop();
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
FrameLayout frame = (FrameLayout) findViewById(R.id.threeD_view);
frame.addView(view);
game=(Game3) getJmeApplication();
}
public MainActivity() {
//appClass = "jme3.innovationtech.co.uk.Game";
appClass = "jme3.innovationtech.co.uk.Game3";
eglConfigType = ConfigType.BEST;
mouseEventsEnabled=false;
exitDialogTitle = "Exit?";
exitDialogMessage = "Press Yes";
eglConfigVerboseLogging = false;
}
/**
- Handle the action of the button being clicked
*/
public void speakButtonClicked(View v)
{
startVoiceRecognitionActivity();
}
/**
- Fire an intent to start the voice recognition activity.
*/
private void startVoiceRecognitionActivity()
{
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Voice recognition Demo…");
startActivityForResult(intent, REQUEST_CODE);
}
/**
- Handle the results from the voice recognition activity.
/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK)
{
// Populate the wordsList with the String values the recognition engine thought it heard
ArrayList<String> matches = data.getStringArrayListExtra(
RecognizerIntent.EXTRA_RESULTS);
String[] strings = matches.toArray(new String[0]);
String command=strings[0];
Toast.makeText(this, "You selected: " + command, Toast.LENGTH_LONG).show();
if (command.equals("move")){
game.animate=true;
}
if (command.equals("stop")){
game.animate=false;
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}[/java]
[java]package jme3.innovationtech.co.uk;
// loaded dancing mesh
// long press spins
// one click changes animation
import android.util.Log;
import com.jme3.animation.AnimChannel;
import com.jme3.animation.AnimControl;
import com.jme3.animation.AnimEventListener;
import com.jme3.animation.LoopMode;
import com.jme3.app.SimpleApplication;
import com.jme3.asset.AssetManager;
import com.jme3.asset.TextureKey;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.PhysicsSpace;
import com.jme3.bullet.collision.PhysicsCollisionEvent;
import com.jme3.bullet.collision.PhysicsCollisionListener;
import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
import com.jme3.bullet.collision.shapes.SphereCollisionShape;
import com.jme3.bullet.control.CharacterControl;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.collision.CollisionResult;
import com.jme3.collision.CollisionResults;
import com.jme3.effect.ParticleEmitter;
import com.jme3.font.BitmapText;
import com.jme3.input.ChaseCamera;
import com.jme3.input.KeyInput;
import com.jme3.input.MouseInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.input.controls.MouseButtonTrigger;
import com.jme3.input.controls.TouchListener;
import com.jme3.input.controls.TouchTrigger;
import com.jme3.input.event.TouchEvent;
import com.jme3.light.AmbientLight;
import com.jme3.light.DirectionalLight;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Matrix3f;
import com.jme3.math.Ray;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Renderer;
import com.jme3.renderer.queue.RenderQueue.ShadowMode;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Box;
import com.jme3.scene.shape.Quad;
import com.jme3.scene.shape.Sphere;
import com.jme3.scene.shape.Sphere.TextureMode;
import com.jme3.texture.Texture;
import com.jme3.texture.Texture.WrapMode;
import com.jme3.ui.Picture;
import com.jme3.util.SkyFactory;
import com.jme3.effect.ParticleMesh;
public class Game3 extends SimpleApplication {
protected Geometry player;
public boolean animate=true;
@Override
public void simpleInitApp() {
Box b = new Box(Vector3f.ZERO, 1, 1, 1);
player = new Geometry("blue cube", b);
Material mat = new Material(assetManager,
"Common/MatDefs/Misc/Unshaded.j3md");
mat.setColor("Color", ColorRGBA.Blue);
player.setMaterial(mat);
rootNode.attachChild(player);
}
/ This is the update loop /
@Override
public void simpleUpdate(float tpf) {
// make the player rotate
if (animate){
player.rotate(0, 2tpf, 0);
}
}
}[/java]
[xml]<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:id="@+id/speakButton"
android:layout_width="fill_parent"
android:onClick="speakButtonClicked"
android:layout_height="wrap_content"
android:text="Voice Command!" />
<FrameLayout
android:id="@+id/threeD_view"
android:layout_height="match_parent"
android:layout_width="match_parent"></FrameLayout>
</LinearLayout>
[/xml]
The trick is to encapsulate the glsurface or jme3 instance in a frame layout. This technique will also be useful for integrating into other existing android apps which use the android UI and its available components. (IE you can add a 3d component/widget)