Android语音识别权限不足(错误代码9) [英] Android Speech Recognition Insufficient Permission (Error Code 9)

查看:349
本文介绍了Android语音识别权限不足(错误代码9)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在没有标准对话框的情况下实现语音识别(该对话框可以正常工作).

I am trying to implement speech recognition without the standard dialog (it is working fine with the dialog).

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

I am getting error code 9 as soon as I try to start listening.

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

My device is an LG G Stylo (running 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
 .....

(也尝试添加INTERNET权限,尽管由于脱机识别应该起作用,所以这不是必需的)

(also tried adding INTERNET permission even though that shouldn't be necessary since offline recognition should be working)

build.gradle:

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){}
}

任何见识都会受到赞赏.

Any insight would be appreciated.

推荐答案

在Android 6上,此权限是危险的权限之一,这意味着您需要要求用户确认(实际上是获得它).检查

On Android 6 this permission is one of dangerous ones which means you need to ask user to confirm it (actually acquire it). Check this and this for more details.

这篇关于Android语音识别权限不足(错误代码9)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆