2014-02-18 46 views
3

我正在用GDK偷偷摸摸地构建一个应用程序,并且无法在沉浸式应用程序中获取语音识别。这是我的第一个Android项目。SpeechRecognizer与玻璃没有足够的权限错误

我试图按照这样的: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> 

紧接触摸事件进来后,我调用开始监听,监听器的onError方法被调用,错误代码为9,表示权限不足。如果任何人有任何有关android语音命令或玻璃语音命令的经验,并知道为什么这会继续失败,我会非常感激。谢谢。

+0

你能把你的清单文件吗? – Dyna

+0

对不起,现在发布了。谢谢参观。 – samgoodness

+0

我不确定这是否是问题,但是您的包名在清单中是:'package =“com.example.medicalglass”'并且您有测试一:'intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,“voice.recognition .test“);'他们不应该是一样的吗? – Dyna

回答

2

开始通过改变这样的代码:

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。

0

语音识别无法在离线状态(没有?),看到这个谷歌眼镜请求的功能允许离线语音识别(Issue 305

1

这应该是与API等级19以上提到的两个权限,现在的工作。

相关问题