SpeechRecognizer insufficient permissions error wi

2019-05-12 01:19发布

I am building an application with the GDK sneak peek and am having trouble getting speech recognition working in an immersive app. This is my first android project.

I tried to follow this: How can I use speech recognition without the annoying dialog in android phones

After making initial progress, I hit a problem where the RecognitionListener class is throwing Error 9, insufficient permissions.

I am using the GDK, which is Android-15.

Initialization of the Recognizer is in my onCreate() method:

sr = SpeechRecognizer.createSpeechRecognizer(this);       
sr.setRecognitionListener(new listener()); 

When I receiver a tap callback, I start listening:

private GestureDetector createGestureDetector(Context context) {
        GestureDetector gestureDetector = new GestureDetector(context);
        //Create a base listener for generic gestures
        gestureDetector.setBaseListener( new GestureDetector.BaseListener() {
            @Override
            public boolean onGesture(Gesture gesture) {
//              Log.info(gesture.name());
                if (gesture == Gesture.TAP) {
                    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);
                    return true;
                }
                return false;
            }
        });

        return gestureDetector;
    }

And here is the definition of my listener class:

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<String> 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);
        }
    }

Here is my manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.medicalglass"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="15" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.medicalglass.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Immediately after the touch event comes in and I call start listening, the listener's onError method is called with error code 9, which denotes insufficient permissions. If anyone has any experience with android speech commands, or glass speech commands and know why this continues to fail I would be very appreciative. Thanks.

4条回答
家丑人穷心不美
2楼-- · 2019-05-12 01:41

Start by changing this code:

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");

To this code:

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());
speechRecognizer.startListening(intent);

EDIT: Add this to your manifest:

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

If you have an error please past your LogCat.

查看更多
爷、活的狠高调
3楼-- · 2019-05-12 01:50

This should be working now with API Level 19 and the two permissions mentioned above.

查看更多
做个烂人
4楼-- · 2019-05-12 01:52

You have to request for permission on Android M onward.

 if (ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(activity, Manifest.permission.GET_ACCOUNTS) == PackageManager.PERMISSION_GRANTED)
                    return;
                else {
                    if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.RECORD_AUDIO)) {
                        Toast.makeText(activity, "Record audio is required", Toast.LENGTH_LONG).show();
                    } else {
                        ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.RECORD_AUDIO}, RECORD_AUDIO);
                    }
                }
查看更多
Luminary・发光体
5楼-- · 2019-05-12 01:56

Voice recognition not available offline (yet?), see this Google Glass requested feature for allowing offline voice recognition (Issue 305)

查看更多
登录 后发表回答