2014-02-18 52 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以上提到的兩個權限,現在的工作。

相關問題