(Android Studio Speech Recognizer) 我收到错误 9(权限不足),即使我已经给了它 RECORD_AUDIO 和 INTERNET

(Android Studio Speech Recognizer) I'm getting error 9 (insufficient Privileges) even though I have given it RECORD_AUDIO and INTERNET

package blessupboys.speechtest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.widget.Button;
import android.widget.TextView;
import java.util.ArrayList;
import android.util.Log;



public class VoiceRecognitionTest extends Activity implements OnClickListener
{

    private TextView mText;
    private SpeechRecognizer sr;
    private static final String TAG = "MyStt3Activity";
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_voice_recognition_test);
        Button speakButton = (Button) findViewById(R.id.btn_speak);
        mText = (TextView) findViewById(R.id.textView1);
        speakButton.setOnClickListener(this);
        sr = SpeechRecognizer.createSpeechRecognizer(this);
        sr.setRecognitionListener(new listener());
    }

    class listener implements RecognitionListener
    {
        public void onReadyForSpeech(Bundle params)
        {
            Log.d(TAG, "onReadyForSpeech");
        }
        public void onBeginningOfSpeech()
        {
            Log.d(TAG, "onBeginningOfSpeech");
        }
        public void onRmsChanged(float rmsdB)
        {
            Log.d(TAG, "onRmsChanged");
        }
        public void onBufferReceived(byte[] buffer)
        {
            Log.d(TAG, "onBufferReceived");
        }
        public void onEndOfSpeech()
        {
            Log.d(TAG, "onEndofSpeech");
        }
        public void onError(int error)
        {
            Log.d(TAG,  "error " +  error);
            mText.setText("error " + error);
        }
        public void onResults(Bundle results)
        {
            String str = new String();
            Log.d(TAG, "onResults " + results);
            ArrayList data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
            for (int i = 0; i < data.size(); i++)
            {
                Log.d(TAG, "result " + data.get(i));
                str += data.get(i);
            }
            mText.setText("results: "+String.valueOf(data.size()));
        }
        public void onPartialResults(Bundle partialResults)
        {
            Log.d(TAG, "onPartialResults");
        }
        public void onEvent(int eventType, Bundle params)
        {
            Log.d(TAG, "onEvent " + eventType);
        }
    }
    public void onClick(View v) {
        if (v.getId() == R.id.btn_speak)
        {
            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,"voice.recognition.test");

            intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,5);
            sr.startListening(intent);
            Log.i("111111","11111111");
        }
    }
}

我的 Android 清单文件如下所示:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="blessupboys.speechtest">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".VoiceRecognitionTest"
            android:label="@string/title_activity_voice_recognition_test"
            android:theme="@style/AppTheme.NoActionBar"></activity>
    </application>

    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

</manifest>

我的日志显示它因 onError() 函数中生成的错误 9 而停止。

01-19 20:04:57.776 18480-18480/? I/art: Not late-enabling -Xcheck:jni (already on)
01-19 20:04:57.925 18480-18480/blessupboys.speechtest W/System: ClassLoader referenced unknown path: /data/app/blessupboys.speechtest-2/lib/x86
01-19 20:04:58.341 18480-18515/blessupboys.speechtest D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
01-19 20:04:58.443 18480-18515/blessupboys.speechtest I/OpenGLRenderer: Initialized EGL, version 1.4
01-19 20:04:58.497 18480-18515/blessupboys.speechtest W/EGL_emulation: eglSurfaceAttrib not implemented
01-19 20:04:58.497 18480-18515/blessupboys.speechtest W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xad79f260, error=EGL_SUCCESS
01-19 20:04:59.033 18480-18480/blessupboys.speechtest I/Choreographer: Skipped 39 frames!  The application may be doing too much work on its main thread.
01-19 20:10:09.965 18480-18480/blessupboys.speechtest I/111111: 11111111
01-19 20:10:10.049 18480-18480/blessupboys.speechtest D/MyStt3Activity: error 9

我正在尝试 运行 这个示例程序来尝试熟悉语音识别器软件。

这可能是由于我 运行 在 android studio 而不是真实设备上安装它造成的问题吗?我非常坚持这一点。

尝试这些链接以获取对您的 project also check all android manifest permissions here and here is a sample project

的引用

希望这些提示有助于解决您的问题!

这只是模拟器不支持的问题。它适用于物理设备。

我发现如果你是 "just a physical device" 并且你仍然得到同样的错误,那很可能是因为如果目标 SDK 在 gradle 文件中。你看到在以后的版本中他们已经改变了危险的许可级别,所以通过降级它,你就可以工作了。

"I am not an expert in this, but this worked from me fine"

如果有人遇到这个问题并且 none 其他答案有帮助,您可能想尝试使用以下代码请求权限:

private void requestRecordAudioPermission() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        String requiredPermission = Manifest.permission.RECORD_AUDIO;

        // If the user previously denied this permission then show a message explaining why
        // this permission is needed
        if (checkCallingOrSelfPermission(requiredPermission) == PackageManager.PERMISSION_DENIED) {
            requestPermissions(new String[]{requiredPermission}, 101);
        }
    }
}

当 google 没有 record_audio 权限时也会抛出错误 9 ERROR_INSUFFICIENT_PERMISSIONS

检查设置 -> 应用程序 -> Google -> 权限 -> 麦克风