问题
We have a demo android application (Android 4.0.3) that runs voice recognition as a service, and (continuosly) logs the results of the recognition on the view.
Everything is working fine in our smartphones.
We would like to replicate this scenario in a Google Glass immersion application, but we always have this error message when we try to start the service:
no selected voice recognition service
Are there some known limitations? Or have someone figured out how to resolve this kind of problem?
Thanks in advance
This is some significant code of the activity:
public class MainActivity extends Activity implements Observer {
...
@Override
protected void onStart() {
super.onStart();
//Toast.makeText(this, "Hi guys", Toast.LENGTH_LONG);
startService(new Intent(this, SilentVoiceRecognitionService.class));
}
...
}
And this is the code of the service:
public class SilentVoiceRecognitionService extends Service {
protected AudioManager mAudioManager;
protected SpeechRecognizer mSpeechRecognizer;
protected Intent mSpeechRecognizerIntent;
protected final Messenger mServerMessenger = new Messenger(new IncomingHandler(this));
private Model model = Model.getInstance();
static final String TAG = "SilentRecognizer";
static final int MSG_RECOGNIZER_START_LISTENING = 1;
static final int MSG_RECOGNIZER_CANCEL = 2;
protected boolean mIsListening;
@Override
public void onCreate()
{
super.onCreate();
mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
mSpeechRecognizer.setRecognitionListener(new SpeechRecognitionListener());
mSpeechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
this.getPackageName());
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("LocalService", "Received start id " + startId + ": " + intent);
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
return START_STICKY;
}
@Override
public void onDestroy()
{
super.onDestroy();
if (mSpeechRecognizer != null)
{
mSpeechRecognizer.destroy();
}
}
protected class SpeechRecognitionListener implements RecognitionListener
{
...
}
protected static class IncomingHandler extends Handler
{
private WeakReference<SilentVoiceRecognitionService> mtarget;
IncomingHandler(SilentVoiceRecognitionService target)
{
mtarget = new WeakReference<SilentVoiceRecognitionService>(target);
}
@Override
public void handleMessage(Message msg)
{
final SilentVoiceRecognitionService target = mtarget.get();
switch (msg.what)
{
case MSG_RECOGNIZER_START_LISTENING:
if (!target.mIsListening)
{
target.mSpeechRecognizer.startListening(target.mSpeechRecognizerIntent);
target.mIsListening = true;
//Log.d(TAG, "message start listening"); //$NON-NLS-1$
}
break;
case MSG_RECOGNIZER_CANCEL:
target.mSpeechRecognizer.cancel();
target.mIsListening = false;
//Log.d(TAG, "message canceled recognizer"); //$NON-NLS-1$
break;
}
}
}
}
回答1:
This feature has been recently accepted but not yet available, see https://code.google.com/p/google-glass-api/issues/detail?id=245
You can load the additional mentioned apk to get the functionality for the mean time. See Using Android Speech Recognition APIs from Google Glass
回答2:
As of XE16 it is now possible to use the SpeechRecognizer directly and get the results through the SpeechRecognitionListener.
Unfortunately this still dosen't work offline.
来源:https://stackoverflow.com/questions/20490921/is-it-possible-to-have-android-voice-recognition-as-a-custom-service-on-google