2016-02-11 57 views
52

當作爲this answer by Iftah說我可以從意圖越來越開放的由語音Recoginition android系統中記錄的聲音傳遞給獲得意圖:Android的實施RecognitionListener

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    // the recording url is in getData: 
    Uri audioUri = data.getData(); 
} 

這裏Intent data有什麼我想要沒有問題。

這一切都完美的作品,但是這種解決方案提出了一個提示的時候說話的用戶,我不想這樣,所以要解決這個問題,我讓我的活動實施RecognitionListener像這樣:

public class MainActivity extends AppCompatActivity implements RecognitionListener { 

    private SpeechRecognizer speech = null; 
    private Intent recognizerIntent; 

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

     recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
     recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "sv_SE"); 
     recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, "sv_SE"); 
     recognizerIntent.putExtra("android.speech.extra.GET_AUDIO_FORMAT", "audio/AMR"); 
     recognizerIntent.putExtra("android.speech.extra.GET_AUDIO", true); 

     speech = SpeechRecognizer.createSpeechRecognizer(this); 
     speech.setRecognitionListener(this); 
     speech.startListening(recognizerIntent); 
    } 

    @Override 
    public void onReadyForSpeech(Bundle params) { 
     Log.d("Debug", "On ready for speech"); 
    } 

    @Override 
    public void onBeginningOfSpeech() { 
     Log.d("Debug", "On beggining of speech"); 
    } 

    @Override 
    public void onRmsChanged(float rmsdB) { 
     Log.d("Debug", "Rsm changed"); 
    } 

    @Override 
    public void onBufferReceived(byte[] buffer) { 
     Log.d("Debug", "Buffer recieved"); 
    } 

    @Override 
    public void onEndOfSpeech() { 
     Log.d("Debug", "On end of speech"); 
    } 

    @Override 
    public void onError(int error) { 
     Log.d("Debug", "Error"); 
    } 

    @Override 
    public void onPartialResults(Bundle partialResults) { 
     Log.d("Debug", "On partial result"); 
    } 

    @Override 
    public void onEvent(int eventType, Bundle params) { 
     Log.d("Debug", "On event"); 
    } 

    @Override 
    public void onResults(Bundle results) { 
     Log.d("Debug", "On result"); 
    } 
} 

這得到各地提示hovewer我無法弄清楚如何獲得烏里像在第一個例子,因爲這裏:

@Override 
    public void onResults(Bundle results) { 
     Log.d("Debug", "On result"); 
     // The results bundle don't contain the URI! 
    } 

我得到一個Bundle results不包含的意圖或URI。我嘗試過查看捆綁包中的所有密鑰,並且沒有URI或Intent存在,我也試過getIntent()但是不返回任何內容。

我感謝任何幫助或推動正確的方向。

+0

您的主要目的是獲取Uri? –

+0

我用它從語音識別中檢索音頻,我知道這是一件麻煩事,但它是今天真正獲得音頻的唯一方法。我可以像第一個例子那樣獲得URI,這就是我自己實現RecognitionListener的時候,我似乎無法得到它。 – Fredrik

+0

我看了文檔,並有一些更多的回調,它給語音緩衝區作爲onBufferReceived(byte []緩衝區) –

回答

1

結果應與關鍵字SpeechRecognizer.RESULTS_RECOGNITION捆綁在一起。

@Override 
public void onResults(Bundle results) { 
    if (results != null && results.containsKey(SpeechRecognizer.RESULTS_RECOGNITION)) { 
     List<String> resultsList = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); 
     // most likely result is resultsList.get(0) 
    } else { 
     // no results 
    } 

} 

請參閱SpeechRecognizer的Android開發者文檔詳細解釋它。這種獲取語音識別結果的方法也適用於部分結果#onPartialResults

+0

這應該是被接受的anwser,請參閱[在線文檔](https://developer.android.com/reference/android/speech/SpeechRecognizer.html#RESULTS_RECOGNITION) –

+0

沒有當我問這個問題時,我是在實際的音頻後語音識別,這是問題,我不明白這是如何解決這個問題。 – Fredrik

+0

而這段時間以前,我並沒有真正的環境或代碼設置來測試它。但是如果有人可以確認這實際上是在沒有用戶提示的情況下獲得音頻和語音識別的結果,那麼我會將其標記爲接受的答案。 – Fredrik