2017-10-09 102 views
-1

我試圖讓我的Android 2.3.3應用程序的語音識別工作,但缺少一些基本的東西。Android正在忽略RECORD_AUDIO權限和語音API實施

首先,我AndroidManifest.xml文件的頂部,我有:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.myuser.myapp"> 
    <uses-permission android:name="android.permission.RECORD_AUDIO" /> 
    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 

     ... remainder omitted for brevity 

其中我相信應促使Android的詢問最終用戶是否要授予我的應用程序權限的話筒時,它啓動(如果沒有,請糾正我!)...

接下來,我有Activity,其中有我的應用程序的語音識別功能的整個需要。基本上,我希望應用程序來檢測每當有人說出聲來,「轉到」:

public class SpeechActivity extends AppCompatActivity implements RecognitionListener { 
    private SpeechRecognizer speechRecognizer; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_speech); 

     int check = ContextCompat.checkSelfPermission(this, android.Manifest.permission.RECORD_AUDIO); 
     if(check != PackageManager.PERMISSION_GRANTED) { 
      throw new RuntimeException("Ya can't do that now, ya here."); 
     } 

     speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this); 
     speechRecognizer.setRecognitionListener(this); 

     Intent speechIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
     speechIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 
     speechIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, "voice.recognition.test"); 
     speechIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 5); 
     speechRecognizer.startListening(speechIntent); 
     Log.i("SpeechActivity", "Speech Recognizer is listening"); 
    } 

    @Override 
    public void onReadyForSpeech(Bundle bundle) { 

    } 

    @Override 
    public void onBeginningOfSpeech() { 

    } 

    @Override 
    public void onRmsChanged(float v) { 

    } 

    @Override 
    public void onBufferReceived(byte[] bytes) { 

    } 

    @Override 
    public void onEndOfSpeech() { 

    } 

    @Override 
    public void onError(int i) { 

    } 

    @Override 
    public void onResults(Bundle bundle) { 
     Log.i("SpeechActivity", "Speech was detected..."); 
     String spoken = ""; 
     ArrayList<String> data = bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); 
     for (int i = 0; i < data.size(); i++) { 
      spoken += data.get(i); 
     } 

     Log.i(this.getClass().getName(), String.format("The word \"%s\" was detected.", spoken)); 
     if(spoken.equalsIgnoreCase("go")) { 
      doSomething(); 
     } 
    } 

    @Override 
    public void onPartialResults(Bundle bundle) { 

    } 

    @Override 
    public void onEvent(int i, Bundle bundle) { 

    } 
} 

我通過(通過USB)我的手機連接到我的筆記本電腦上運行我的三星Galaxy S7邊緣應用,點擊Android Studio中的運行(app)按鈕,然後選擇我的手機作爲連接的設備。

當應用程序在我的手機上啓動時,我注意到的第一件事是它不會提示我接受/拒絕應用程序使用麥克風的權限。這是一個預警信號,告訴我有什麼東西是錯誤的。

但是,當我瀏覽到SpeechActivity/activity_speech.xml屏幕,我得到:

在運行此我得到:

FATAL EXCEPTION: main 
Process: com.example.myuser.myapp, PID: 9585 
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myuser.myapp/com.example.myuser.myapp.SpeechActivity}: java.lang.RuntimeException: Ya can't do that now, ya here. 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2947) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3008) 
    at android.app.ActivityThread.-wrap14(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1650) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6688) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358) 
Caused by: java.lang.RuntimeException: Ya can't do that now, ya here. 
    at com.example.myuser.myapp.SpeechActivity.onCreate(SpeechActivity.java:43) 
    at android.app.Activity.performCreate(Activity.java:6912) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2900) 

是否有可能我需要下載和安裝,以插件或APK讓語音識別工作?爲什麼我的應用程序沒有要求使用手機麥克風的權限?語音識別器是如何開始/聆聽的,但似乎沒有聽到任何演講?

+0

聲明清單中的權限是Mashmallow之前的方式,因爲您需要通過權限詢問用戶權限,以便在屏幕上顯示提示。你應該檢查這個鏈接:https://developer.android.com/training/permissions/requesting.html – vincenth

+0

三星Galaxy S7 Edge與Android 2.3.3,真的嗎? – rupinderjeet

+0

是@rupinderjeet 2.3.3爲什麼我的特定設備出現問題? – smeeb

回答

3

根據the documentation,這是一個危險的許可。您應該向用戶請求。

RECORD_AUDIO:允許應用程序記錄音頻。

防護等級:危險

常數值: 「android.permission.RECORD_AUDIO」

簡單地說,你可以檢查你的應用程序有使用

int permissionCheck = ContextCompat.checkSelfPermission(thisActivity, 
    Manifest.permission.RECORD_AUDIO); 

if (permissionCheck == PERMISSION_GRANTED) { 
    // you have the permission, proceed to record audio 

    speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this); 
    speechRecognizer.setRecognitionListener(this); 

    Intent speechIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
    speechIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 
    speechIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, "voice.recognition.test"); 
    speechIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 5); 
    speechRecognizer.startListening(speechIntent); 
    Log.i("SpeechActivity", "Speech Recognizer is listening"); 

} 
else { 

    // you don't have permission, try requesting for it 

    ActivityCompat.requestPermissions(thisActivity, 
      new String[]{Manifest.permission.RECORD_AUDIO}, 
      MY_PERMISSION_REQUEST_RECORD_AUDIO); 
} 

把你的時間這個權限,在Android指南中閱讀Requesting Permissions指南。

+0

謝謝@rupinderjeet(+1;我也編輯了我以前的評論,因爲你修改後的答案更符合我的問題!)。請參閱我的編輯,我添加了您的建議權限檢查,現在當我導航到「SpeechActivity」時拋出異常(「Ya不能那麼做......」),我的應用程序就會死亡。此外,它仍然不會促使我授權使用麥克風。有任何想法嗎? – smeeb

+0

它會拋出異常,因爲您沒有權限。我編輯了我的答案,告訴你如何請求權限。另外,請注意我的回答的最後一行 – rupinderjeet

+0

謝謝@rupinderjeet(+1) - 我添加了堆棧跟蹤如果它可以幫助你。你爲什麼說我沒有權限?我不應該在某個時候獲得許可嗎? – smeeb