我的代碼工作正常上Nexus5x(牛軋糖)和Nexus9(牛軋糖)
嘗試和表演的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" />
是U使用的是什麼設備?許多nugat設備不支持一些64位應用程序,即使他們說他們這樣做。嘗試用32bit構建它。我有類似的問題,並通過這種方式解決它。 –
API約2年沒有變化(請參閱https://android.googlesource.com/platform/frameworks/base/+log/refs/heads/master/core/java/android/speech)。你正在描述這個API的實現中的一個問題,但是你沒有提及它的實現(名稱,版本號等)。Android本身不包含語音識別器實現,儘管許多手機都預先安裝了谷歌的實現。 – Kaarel
三星G7與Android 7,語音不起作用 – James