如Iftah的这个答案所述,我可以通过获取传递给以下Intent的Uri来获取Android中语音识别记录的音频:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// the recording url is in getData:
Uri audioUri = data.getData();
}
在这里,Intent数据
恰好包含了我想要的内容,没有问题。
所有东西都工作得非常完美,但是这个解决方案会为用户提供一个提示,让他们选择何时说话,我不想要那样,所以我让我的活动实现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");
}
}
这种方法可以避开提示,但是我无法像第一个例子中那样获得Uri,因为在这里:
@Override
public void onResults(Bundle results) {
Log.d("Debug", "On result");
// The results bundle don't contain the URI!
}
我得到了一个不包含Intent或Uri的Bundle结果。我已经尝试查看bundle中的所有键,但没有URI或Intent存在。我还尝试了getIntent(),但没有返回任何内容。
我很感谢任何帮助或指导。
ACTION_VOICE_SEARCH_HANDS_FREE
操作启动意图?因为API明确建议“启动一个活动,该活动将提示用户进行语音输入,而不需要用户的视觉注意或触摸输入。”,来源:http://developer.android.com/reference/android/speech/RecognizerIntent.html#ACTION_RECOGNIZE_SPEECH - Pragnani