我试图在没有标准对话框的情况下实现语音识别(它可以很好地处理这个对话框)。
当我试图开始监听时,我就会得到错误代码9。
我的设备是LG G(运行Android6.0)。
清单:
<manifest package="example.com.myapplication"
xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<application
.....
(还尝试添加INTERNET权限,尽管这不应该是必要的,因为离线识别应该有效)
build.gradle:
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "example.com.appname"
minSdkVersion 19
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
语音识别代码:
private SpeechRecognizer speechRecognizer;
protected void onCreate(Bundle savedInstanceState) {
speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
speechRecognizer.setRecognitionListener(new speech_listener());
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,
getApplication().getPackageName());
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.ENGLISH);
speechRecognizer.startListening(intent);
侦听器(内部)类:
class speech_listener implements RecognitionListener
{
public void onReadyForSpeech(Bundle params){}
public void onBeginningOfSpeech(){}
public void onRmsChanged(float rmsdB){}
public void onBufferReceived(byte[] buffer){}
public void onEndOfSpeech(){}
public void onError(int error){
Log.d("Speech", "error: " + error);
}
public void onResults(Bundle results)
{
ArrayList data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
String answer = (String)data.get(0);
processAnswer(answer);
}
public void onPartialResults(Bundle partialResults){}
public void onEvent(int eventType, Bundle params){}
}
任何洞察力都将不胜感激。
发布于 2016-02-07 06:29:44
在Android 6上,这种许可是危险的,这意味着您需要要求用户确认它(实际上获得了它)。有关更多细节,请查看这和这。
发布于 2016-03-03 22:59:14
发布于 2021-07-01 12:32:44
在我的例子中,错误消息是“9/不足的权限”。
通过给麦克风许可给Google应用程序解决问题
参考资料:https://github.com/react-native-voice/voice/issues/253#issuecomment-812726040
https://stackoverflow.com/questions/35248075
复制相似问题