我的代码在Nexus5x(Nougat)和Nexus9(Nougat)上运行良好。
尝试并显示logcat日志。
SpeechRecognizer mGoogleSr;
void initGoogleSr(Context context) {
mGoogleSr = SpeechRecognizer.createSpeechRecognizer(context);
mGoogleSr.setRecognitionListener(new GoogleSrListener());
}
void startGoogleSr() {
if (mGoogleSr != null) {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getPackageName());
intent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);
mGoogleSr.startListening(intent);
}
}
void cancelRecognizing() {
if (mGoogleSr != null) {
mGoogleSr.cancel();
}
}
public class GoogleSrListener implements RecognitionListener {
String lastPartialText;
@Override
public void onReadyForSpeech(Bundle params) {
Log.v(TAG, ">>> onReadyForSpeech");
showMessage("ready");
}
@Override
public void onBeginningOfSpeech() {
Log.v(TAG, ">>> onBeginningOfSpeech");
showMessage("recognizing");
}
@Override
public void onRmsChanged(float rmsdB) {
}
@Override
public void onBufferReceived(byte[] buffer) {
}
@Override
public void onEndOfSpeech() {
Log.v(TAG, ">>> onEndOfSpeech");
showMessage("waiting result");
}
@Override
public void onError(int error) {
Log.v(TAG, ">>> onError : " + error);
switch (error) {
case SpeechRecognizer.ERROR_AUDIO:
Log.e(TAG, "ERROR_AUDIO");
break;
case SpeechRecognizer.ERROR_CLIENT:
Log.e(TAG, "ERROR_CLIENT");
break;
case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS:
Log.e(TAG, "ERROR_INSUFFICIENT_PERMISSIONS");
break;
case SpeechRecognizer.ERROR_NETWORK:
Log.e(TAG, "ERROR_NETWORK");
break;
case SpeechRecognizer.ERROR_NETWORK_TIMEOUT:
Log.e(TAG, "ERROR_NETWORK_TIMEOUT");
break;
case SpeechRecognizer.ERROR_RECOGNIZER_BUSY:
Log.e(TAG, "ERROR_RECOGNIZER_BUSY");
break;
case SpeechRecognizer.ERROR_SERVER:
Log.e(TAG, "ERROR_SERVER");
break;
case SpeechRecognizer.ERROR_NO_MATCH:
Log.v(TAG, "ERROR_NO_MATCH");
break;
case SpeechRecognizer.ERROR_SPEECH_TIMEOUT:
Log.v(TAG, "ERROR_SPEECH_TIMEOUT");
break;
default:
Log.v(TAG, "ERROR_UNKOWN");
}
}
@Override
public void onPartialResults(Bundle partialResults) {
Log.v(TAG, ">>> onPartialResults");
List<String> resultList = partialResults.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
if (resultList != null) {
String text = resultList.get(0);
if (text.equals(lastPartialText)) {
return;
}
lastPartialText = text;
Log.v(TAG, "partial : " + text);
}
}
@Override
public void onResults(Bundle results) {
Log.v(TAG, ">>> onResults");
List<String> resultList = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
if (resultList != null) {
String text = resultList.get(0);
Log.v(TAG, "result : " + text);
showMessage(text);
}
}
@Override
public void onEvent(int eventType, Bundle params) {
Log.v(TAG, ">>> onEvent type = " + eventType);
}
}
清单文件中的权限(可能会有冗余):
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
SpeechRecognizer.isRecognitionAvailable(mContext)
。你确认在三星设备上没有将提供程序设置为Vlingo吗?虽然代码会告诉你它可以工作,但实际上并不行。Google的实现每次发布都变得更糟。如果你想自己处理所有事情,我建议切换到他们的Cloud Speech API。 - brandall