我正在尝试测试一个基于Google API实现语音识别的开源应用程序。 该应用程序似乎可以在真实电话上运行,但是在虚拟环境中,它会因网络错误而失败(SpeechRecognizer.ERROR_NETWORK,代码2)。 我看到了许多类似的问题,并且在一半的答案中,建议使用真实的电话。但是,这些回复来自2014年及之前。 现在在2020年左右,情况会发生变化吗?一个如何使用AVD测试语音识别应用程序?
我正在使用虚拟Nexus 6 API 26(Android 8.0,Google API),x86 CPU / ABI。
我已经将Google Play安装到模拟器中,并尝试了一些录音应用程序-它们工作正常。但是,即使授予了麦克风许可,另一个语音识别应用也无法正常工作(无法听到声音?)。
代码:
package com.lightbuzz.speechrecognitionandroid;
import android.content.Intent;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements RecognitionListener {
private TextView textViewResults;
protected Intent intent;
protected SpeechRecognizer recognizer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textViewResults = (TextView)findViewById(R.id.textViewResults);
intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
intent.putExtra(RecognizerIntent.EXTRA_WEB_SEARCH_ONLY, "false");
intent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS, "3000");
recognizer = SpeechRecognizer.createSpeechRecognizer(this);
recognizer.setRecognitionListener(this);
recognizer.startListening(intent);
}
@Override
public void onReadyForSpeech(Bundle params) {
textViewResults.setText("Listening...");
}
@Override
public void onBeginningOfSpeech() {
}
@Override
public void onRmsChanged(float rmsdB) {
}
@Override
public void onBufferReceived(byte[] buffer) {
}
@Override
public void onEndOfSpeech() {
}
@Override
public void onError(int error) {
Boolean restart = false;
String message;
switch (error) {
case SpeechRecognizer.ERROR_AUDIO:
message = "Audio error";
break;
case SpeechRecognizer.ERROR_CLIENT:
message = "Client error";
break;
case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS:
message = "Insufficient permissions";
break;
case SpeechRecognizer.ERROR_NETWORK:
message = "Network error";
/// ** after initialisation it stucks here for several times and then the app exits **
restart = true;
break;
case SpeechRecognizer.ERROR_NETWORK_TIMEOUT:
message = "Network timeout";
break;
case SpeechRecognizer.ERROR_NO_MATCH:
message = "No match";
break;
case SpeechRecognizer.ERROR_RECOGNIZER_BUSY:
message = "Speech Recognizer is busy";
break;
case SpeechRecognizer.ERROR_SERVER:
message = "Server error";
break;
case SpeechRecognizer.ERROR_SPEECH_TIMEOUT:
message = "No speech input";
break;
default:
message = "Speech Recognizer cannot understand you";
break;
}
textViewResults.setText(message);
if (restart) {
recognizer.stopListening();
recognizer.startListening(intent);
}
}
@Override
public void onResults(Bundle results) {
ArrayList<String> words = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
String text = "";
for (String word : words) {
text += word + " ";
}
textViewResults.setText(text);
recognizer.stopListening();
recognizer.startListening(intent);
}
}
... some stub code was removed ...