Android MediaRecorder到AudioTrack,录制和播放

时间:2011-07-07 20:18:10

标签: android audio playback recording mediarecorder

我正在尝试将其录制到可以使用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;
    }
}

1 个答案:

答案 0 :(得分:0)

将文件写入音轨是什么意思?您不需要将任何文件写入音轨,您设置音频源,录音机将创建音轨并从音频源读取pcm数据,然后编码数据,将数据写入输出文件。