Android语音识别权限不足(错误代码9)

Mar*_*ser 6 android speech-recognition android-permissions

我试图在没有标准对话框的情况下实现语音识别(它在对话框中工作正常).

我尝试开始收听时收到错误代码9.

我的设备是LG G Stylo(运行Android 6.0).

表现:

<manifest package="example.com.myapplication"
      xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<application
 .....
Run Code Online (Sandbox Code Playgroud)

(也尝试添加INTERNET权限,即使这不应该是必要的,因为离线识别应该正常工作)

的build.gradle:

compileSdkVersion 23
buildToolsVersion "23.0.2"

defaultConfig {
    applicationId "example.com.appname"
    minSdkVersion 19
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
}
Run Code Online (Sandbox Code Playgroud)

语音识别码:

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);
Run Code Online (Sandbox Code Playgroud)

监听器(内部)类:

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){}
}
Run Code Online (Sandbox Code Playgroud)

任何见解将不胜感激.

WeN*_*igh 13

添加到Sam的答案:当您在Android 6上开发应用程序时,可能不会提示您批准"危险"的录制音频(麦克风)权限,因此您需要在"设置"中手动打开应用程序并授予权限.

手动授予应用权限的屏幕截图


Sam*_*Sam 12

在Android 6上,此权限是危险的权限之一,这意味着您需要让用户确认它(实际获取它).检查这个这个的更多细节.


Tha*_*P A 5

就我而言,错误消息是“9/权限不足”

通过向Google应用授予麦克风权限来解决

在此输入图像描述

参考:https://github.com/react-native-voice/voice/issues/253#issuecomment-812726040