5

我们有一个演示 android 应用程序 (Android 4.0.3) 将语音识别作为服务运行,并(持续)在视图上记录识别结果。

我们的智能手机一切正常。

我们想在 Google Glass 沉浸式应用程序中复制这个场景,但是当我们尝试启动服务时总是会出现以下错误消息:

未选择语音识别服务

有一些已知的限制吗?或者有人想出如何解决这种问题?

提前致谢

这是该活动的一些重要代码:

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));
   }
   ...
}

这是服务的代码:

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;
            }
      } 
   }

}

4

2 回答 2

3

此功能最近已被接受但尚不可用,请参阅https://code.google.com/p/google-glass-api/issues/detail?id=245

您可以同时加载提到的附加 apk 以获取该功能。请参阅使用来自 Google Glass 的 Android 语音识别 API

于 2013-12-14T03:38:57.673 回答
0

从 XE16 开始,现在可以直接使用 SpeechRecognizer 并通过 SpeechRecognitionListener 获取结果。

不幸的是,这仍然无法离线工作。

于 2014-04-17T23:01:14.590 回答