Android MediaRecorder 到 AudioTrack、录音和播放
我正在尝试使用 MediaRecorder 和 AudioTrack 录制用户语音并在同一活动中回放。我只是不明白如何将文件写入 AudioTrack。我已经阅读了两者的文档,但根本无法弄清楚。任何帮助将不胜感激。到目前为止,这是我的代码,它还不完整。您需要读取的唯一按钮是 recordButton 和playbackButton。谢谢!
private File outputFile = null;
private AudioTrack voice = null;
private MediaRecorder recorder = null;
....
// Setup recorder...
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
// Setup record file...
outputFile = getFileStreamPath("output.amr");
recorder.setOutputFile(outputFile.getAbsolutePath());
public void onClick(View v){
switch(v.getId()) {
case R.id.next_button:
giveSentence();
break;
case R.id.repeat_button:
// playSentence();
break;
case R.id.recordButton:
if (!recording){
recordButton2.setBackgroundResource(android.R.drawable.button_onoff_indicator_on);
recording = true;
recorder.reset();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(outputFile.getAbsolutePath());
try {
recorder.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
recorder.start();
}
else if(recording) {
recordButton2.setBackgroundResource(android.R.drawable.button_onoff_indicator_off);
recording = false;
recorder.stop();
}
break;
case R.id.playbackButton:
Music.playSentence(this, outputFile);
break;
case R.id.slowButton:
if(!slowedSpeech) {
slowButton2.setBackgroundResource(android.R.drawable.ic_dialog_alert);
slowedSpeech = true;
// slowspeech();
}
else if(slowedSpeech) {
slowButton2.setBackgroundResource(android.R.drawable.ic_menu_recent_history);
slowedSpeech = false;
// noSlowSpeech();
}
break;
}
}
I'm trying to make it to where I can record the users voice and play it back in the same activity using the MediaRecorder and AudioTrack. I just don't understand how to write the file to AudioTrack. I've read the documents on both and simply can't figure it out. Any help would be appreciated. Here's my code so far, it's not complete. The only buttons you need to read are recordButton and playbackButton. Thanks!
private File outputFile = null;
private AudioTrack voice = null;
private MediaRecorder recorder = null;
....
// Setup recorder...
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
// Setup record file...
outputFile = getFileStreamPath("output.amr");
recorder.setOutputFile(outputFile.getAbsolutePath());
public void onClick(View v){
switch(v.getId()) {
case R.id.next_button:
giveSentence();
break;
case R.id.repeat_button:
// playSentence();
break;
case R.id.recordButton:
if (!recording){
recordButton2.setBackgroundResource(android.R.drawable.button_onoff_indicator_on);
recording = true;
recorder.reset();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(outputFile.getAbsolutePath());
try {
recorder.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
recorder.start();
}
else if(recording) {
recordButton2.setBackgroundResource(android.R.drawable.button_onoff_indicator_off);
recording = false;
recorder.stop();
}
break;
case R.id.playbackButton:
Music.playSentence(this, outputFile);
break;
case R.id.slowButton:
if(!slowedSpeech) {
slowButton2.setBackgroundResource(android.R.drawable.ic_dialog_alert);
slowedSpeech = true;
// slowspeech();
}
else if(slowedSpeech) {
slowButton2.setBackgroundResource(android.R.drawable.ic_menu_recent_history);
slowedSpeech = false;
// noSlowSpeech();
}
break;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将文件写入音轨是什么意思?您不需要向音轨写入任何文件,您设置音频源,录音机将创建音轨并从音频源读取pcm数据,然后对数据进行编码,将数据写入输出文件。
what do you mean by write file to audio track? you don't need to write any file to audio track, you set the audio source, the recorder will create audio track and read pcm data from the audio source, then encoder the data, write the data into the output file.