我已经成功地将连续语音识别工作(使用SpeechRecognizer类)作为服务,适用于4.1以上的所有Android版本.我的问题是关于让它在版本4.1和4.2上运行,因为众所周知,有一个问题是,API没有按照文档识别启动后几秒钟的记录,如果没有检测到语音输入那么它就像如果语音识别器无声地死亡.(http://code.google.com/p/android/issues/detail?id=37883)
我找到了一个问题,提出解决这个问题的方法(语音识别在几秒钟后停止监听),但我不确定如何实现此解决方案所需的处理程序.我知道这种解决方法每隔几秒钟会发生一次"嘟嘟"声,但对我来说,获得连续的语音识别更为重要.
如果有人有任何其他替代解决方法,那么我也想听听.
我创建了一个简单的语音识别服务:为此,我创建了一个子类,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;
/**
*
* …Run Code Online (Sandbox Code Playgroud)