3 回答
TA贡献1878条经验 获得超4个赞
@Kaarel的答案几乎是完整的-产生的音频已经intent.getData()可以使用读取ContentResolver
不幸的是,返回的AMR文件质量很低-我找不到能够获得高质量记录的方法。除“ audio / AMR”外,我尝试过的任何值在中都返回null intent.getData()。
如果您找到获取高质量录音的方法-请发表评论或添加答案!
public void startSpeechRecognition() {
// Fire an intent to start the speech recognition activity.
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
// secret parameters that when added provide audio url in the result
intent.putExtra("android.speech.extra.GET_AUDIO_FORMAT", "audio/AMR");
intent.putExtra("android.speech.extra.GET_AUDIO", true);
startActivityForResult(intent, "<some code you choose>");
}
// handle result of speech recognition
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// the resulting text is in the getExtras:
Bundle bundle = data.getExtras();
ArrayList<String> matches = bundle.getStringArrayList(RecognizerIntent.EXTRA_RESULTS)
// the recording url is in getData:
Uri audioUri = data.getData();
ContentResolver contentResolver = getContentResolver();
InputStream filestream = contentResolver.openInputStream(audioUri);
// TODO: read audio file from inputstream
}
- 3 回答
- 0 关注
- 501 浏览
添加回答
举报