Android 正在忽略 RECORD_AUDIO 权限和语音 API 实施

Android is ignoring RECORD_AUDIO permission and Speech API implementation

我正在尝试让语音识别为我的 Android 2.3.3 应用程序工作,但缺少一些基本的东西。

首先,在我的 AndroidManifest.xml 文件的顶部,我有:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myuser.myapp">
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"

        ... remainder omitted for brevity

相信 应该提示 Android 询问最终用户他们是否希望在启动时将我的应用程序权限授予 microphone向上(如果不对,请指正!)...

接下来,我有 Activity,它满足了我的应用程序语音识别功能的全部需求。基本上,我希望该应用程序能够检测到有人大声说“Go”:

public class SpeechActivity extends AppCompatActivity implements RecognitionListener {
    private SpeechRecognizer speechRecognizer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_speech);

        int check = ContextCompat.checkSelfPermission(this, android.Manifest.permission.RECORD_AUDIO);
        if(check != PackageManager.PERMISSION_GRANTED) {
            throw new RuntimeException("Ya can't do that now, ya here.");
        }

        speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
        speechRecognizer.setRecognitionListener(this);

        Intent speechIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        speechIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        speechIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, "voice.recognition.test");
        speechIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 5);
        speechRecognizer.startListening(speechIntent);
        Log.i("SpeechActivity", "Speech Recognizer is listening");
    }

    @Override
    public void onReadyForSpeech(Bundle bundle) {

    }

    @Override
    public void onBeginningOfSpeech() {

    }

    @Override
    public void onRmsChanged(float v) {

    }

    @Override
    public void onBufferReceived(byte[] bytes) {

    }

    @Override
    public void onEndOfSpeech() {

    }

    @Override
    public void onError(int i) {

    }

    @Override
    public void onResults(Bundle bundle) {
        Log.i("SpeechActivity", "Speech was detected...");
        String spoken = "";
        ArrayList<String> data = bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
        for (int i = 0; i < data.size(); i++) {
            spoken += data.get(i);
        }

        Log.i(this.getClass().getName(), String.format("The word \"%s\" was detected.", spoken));
        if(spoken.equalsIgnoreCase("go")) {
            doSomething();
        }
    }

    @Override
    public void onPartialResults(Bundle bundle) {

    }

    @Override
    public void onEvent(int i, Bundle bundle) {

    }
}

我 运行 通过将我的 phone 连接到我的笔记本电脑(通过 USB),点击 运行(应用程序),在我的三星 Galaxy S7 Edge 上安装应用程序 Studio 中的 Android 按钮,并选择我的 phone 作为连接的设备。

当应用程序在我的 phone 上启动时,我注意到的第一件事是 它没有提示我 accept/reject 应用程序使用 microphone. 这对我来说是一个预警信号,表明出现了问题。

但是,当我导航到 SpeechActivity/activity_speech.xml 屏幕时,我得到:

当这个 运行s 我得到:

FATAL EXCEPTION: main
Process: com.example.myuser.myapp, PID: 9585
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myuser.myapp/com.example.myuser.myapp.SpeechActivity}: java.lang.RuntimeException: Ya can't do that now, ya here.
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2947)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3008)
   at android.app.ActivityThread.-wrap14(ActivityThread.java)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1650)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:154)
   at android.app.ActivityThread.main(ActivityThread.java:6688)
   at java.lang.reflect.Method.invoke(Native Method)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
Caused by: java.lang.RuntimeException: Ya can't do that now, ya here.
   at com.example.myuser.myapp.SpeechActivity.onCreate(SpeechActivity.java:43)
   at android.app.Activity.performCreate(Activity.java:6912)
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2900)

我是否需要下载并安装插件或 APK 才能使语音识别正常工作?为什么我的应用程序不请求使用我 phone 的麦克风的权限?语音识别器如何 started/listening,但似乎根本无法识别任何语音?

根据the documentation,这是一个危险的权限。您应该向用户索取。

RECORD_AUDIO : Allows an application to record audio.

Protection level: dangerous

Constant Value: "android.permission.RECORD_AUDIO"

简单地说,您可以使用

检查您的应用是否具有此权限
int permissionCheck = ContextCompat.checkSelfPermission(thisActivity,
    Manifest.permission.RECORD_AUDIO);

if (permissionCheck == PERMISSION_GRANTED) {
    // you have the permission, proceed to record audio

    speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
    speechRecognizer.setRecognitionListener(this);

    Intent speechIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    speechIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    speechIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, "voice.recognition.test");
    speechIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 5);
    speechRecognizer.startListening(speechIntent);
    Log.i("SpeechActivity", "Speech Recognizer is listening");

} 
else {

    // you don't have permission, try requesting for it

    ActivityCompat.requestPermissions(thisActivity,
            new String[]{Manifest.permission.RECORD_AUDIO},
            MY_PERMISSION_REQUEST_RECORD_AUDIO);
}

慢慢来,阅读 Android 指南中的 Requesting Permissions 指南。