當作爲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()
但是不返回任何內容。
我感謝任何幫助或推動正確的方向。
您的主要目的是獲取Uri? –
我用它從語音識別中檢索音頻,我知道這是一件麻煩事,但它是今天真正獲得音頻的唯一方法。我可以像第一個例子那樣獲得URI,這就是我自己實現RecognitionListener的時候,我似乎無法得到它。 – Fredrik
我看了文檔,並有一些更多的回調,它給語音緩衝區作爲onBufferReceived(byte []緩衝區) –