【问题标题】:SpeechRecognizer insufficient permissions error with GlassGlass 的 SpeechRecognizer 权限不足错误
【发布时间】:2014-02-18 16:05:19
【问题描述】:

我正在使用 GDK 先睹为快构建应用程序,但无法让语音识别在沉浸式应用程序中工作。这是我的第一个安卓项目。

我试着关注这个:How can I use speech recognition without the annoying dialog in android phones

在取得初步进展后,我遇到了 RecognitionListener 类抛出错误 9,权限不足的问题。

我使用的是 GDK,它是 Android-15。

识别器的初始化在我的 onCreate() 方法中:

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

当我收到点击回调时,我开始收听:

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

这是我的监听器类的定义:

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

这是我的清单文件:

<?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>

touch事件进来后,我调用start listener,立即调用listener的onError方法,错误码9,表示权限不足。如果有人对 android 语音命令或玻璃语音命令有任何经验并且知道为什么这会继续失败,我将非常感激。谢谢。

【问题讨论】:

  • 你能把你的清单文件放进去吗?
  • 对不起,它现在发布。感谢您的观看。
  • 我不确定这是不是问题,但你在清单中的包名是:package="com.example.medicalglass",这里你有一个测试:intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,"voice.recognition.test"); 他们不应该是一样的吗?
  • 我进行了更改,但它仍然告诉我我没有录音权限。
  • 请在下面查看我的答案。

标签: android speech-recognition google-glass google-gdk


【解决方案1】:

首先更改此代码:

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

编辑: 将此添加到您的清单中:

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

如果您有错误,请通过您的 LogCat。

【讨论】:

    【解决方案2】:

    现在应该可以使用 API 级别 19 和上面提到的两个权限。

    【讨论】:

      【解决方案3】:

      您必须在 Android M 上请求权限。

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

      【讨论】:

        【解决方案4】:

        语音识别无法离线使用(还没有?),请参阅 Google Glass 请求的允许离线语音识别的功能 (Issue 305)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-10-24
          • 1970-01-01
          • 1970-01-01
          • 2016-05-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-10-31
          相关资源
          最近更新 更多