我创建了一个简单的语音识别服务:为此我创建了一个
android.speech.RecognitionService的子类,并创建了一个活动来启动和停止此服务.
我的自定义语音识别服务通常使用默认的语音识别器,因为我的目标只是理解RecognitionService和RecognitionService.Callback类的工作原理.
public class SimpleVoiceService extends RecognitionService { private SpeechRecognizer m_EngineSR; @Override public void onCreate() { super.onCreate(); Log.i("SimpleVoiceService","Service started"); } @Override public void onDestroy() { super.onDestroy(); Log.i("SimpleVoiceService","Service stopped"); } @Override protected void onCancel(Callback listener) { m_EngineSR.cancel(); } @Override protected void onStartListening(Intent recognizerIntent,Callback listener) { m_EngineSR.setRecognitionListener(new VoiceResultsListener(listener)); m_EngineSR.startListening(recognizerIntent); } @Override protected void onStopListening(Callback listener) { m_EngineSR.stopListening(); } /** * */ private class VoiceResultsListener implements RecognitionListener { private Callback m_UserSpecifiedListener; /** * * @param userSpecifiedListener */ public VoiceResultsListener(Callback userSpecifiedListener) { m_UserSpecifiedListener = userSpecifiedListener; } @Override public void onBeginningOfSpeech() { try { m_UserSpecifiedListener.beginningOfSpeech(); } catch (RemoteException e) { e.printStackTrace(); } } @Override public void onBufferReceived(byte[] buffer) { try { m_UserSpecifiedListener.bufferReceived(buffer); } catch (RemoteException e) { e.printStackTrace(); } } @Override public void onEndOfSpeech() { try { m_UserSpecifiedListener.endOfSpeech(); } catch (RemoteException e) { e.printStackTrace(); } } @Override public void onError(int error) { try { m_UserSpecifiedListener.error(error); } catch (RemoteException e) { e.printStackTrace(); } } @Override public void onEvent(int eventType,Bundle params) { ; } @Override public void onPartialResults(Bundle partialResults) { try { m_UserSpecifiedListener.partialResults(partialResults); } catch (RemoteException e) { e.printStackTrace(); } } @Override public void onReadyForSpeech(Bundle params) { try { m_UserSpecifiedListener.readyForSpeech(params); } catch (RemoteException e) { e.printStackTrace(); } } @Override public void onResults(Bundle results) { try { m_UserSpecifiedListener.results(results); } catch (RemoteException e) { e.printStackTrace(); } } @Override public void onRmsChanged(float rmsdB) { try { m_UserSpecifiedListener.rmsChanged(rmsdB); } catch (RemoteException e) { e.printStackTrace(); } } } }
我使用以下活动启动和停止服务.
public class VoiceServiceStarterActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Button startButton = new Button(this); startButton.setText("Start the service"); startButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startVoiceService(); } }); Button stopButton = new Button(this); stopButton.setText("Stop the service"); stopButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { stopVoiceService(); } }); LinearLayout layout = new LinearLayout(this); layout.setOrientation(LinearLayout.VERTICAL); layout.addView(startButton); layout.addView(stopButton); setContentView(layout); } private void startVoiceService() { startService(new Intent(this,SimpleVoiceService.class)); } private void stopVoiceService() { stopService(new Intent(this,SimpleVoiceService.class)); } }
最后,我在AndroidManifest.xml上声明了我的服务(请参阅Android SDK文件夹中的VoiceRecognition示例).
<service android:name="SimpleVoiceService" android:label="@string/service_name" > <intent-filter> <action android:name="android.speech.RecognitionService" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </service>
然后我在Android设备上安装了这个应用程序,然后启动它:
– 当我启动服务时,它正常启动;
– 当我停下来时,它会正常停止.
但是,如果我在另一个活动中启动以下代码,则活动List仅包含一个元素,这是默认的语音识别器.
PackageManager pm = getPackageManager(); List<ResolveInfo> activities = pm.queryIntentActivities( new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),0);
为什么我的语音识别器不会返回系统中的那些人?
解决方法
如果你想使用queryIntentActivities(新的Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),0)来获取你的活动(VoiceServiceStarterActivity),那么你必须在应用程序的Manifest中声明这个活动处理RecognizerIntent.ACTION_RECOGNIZE_SPEECH,就像这样
<activity android:name="VoiceServiceStarterActivity"> <intent-filter> <action android:name="android.speech.action.RECOGNIZE_SPEECH" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> ... </activity>
对于更具体的代码,请查看项目Kõnele(source code),该项目本质上是在Android上提供语音识别的接口的开源实现,即它包括:
> ACTION_RECOGNIZE_SPEECH
> ACTION_WEB_SEARCH
> RecognitionService
并使用开源语音识别服务器.