Android的MediaRecorder到AudioTrack,录制和播放 [英] Android MediaRecorder to AudioTrack, Recording and Playback

查看:198
本文介绍了Android的MediaRecorder到AudioTrack,录制和播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使它在那里我可以记录用户的声音,并在使用MediaRecorder和AudioTrack相同的活动回放。我只是不知道如何将文件写入AudioTrack。我读过两个文​​件根本无法弄明白。任何帮助将是AP preciated。这里是我的code,到目前为止,这是不完整的。你需要阅读的唯一的按钮是recordButton和playbackButton。谢谢!

 私人文件OUTPUTFILE = NULL;
私人AudioTrack声音= NULL;
私人MediaRecorder记录= NULL;
....
        //设置录音设备...
    记录=新MediaRecorder();
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    recorder.setAudioEn codeR(MediaRecorder.AudioEn coder.AMR_NB);
    //设置记录文件...
    OUTPUTFILE = getFileStreamPath(output.amr);
    recorder.setOutputFile(outputFile.getAbsolutePath());
公共无效的onClick(视图v){
    开关(v.getId()){
    案例R.id.next_button:
        giveSentence();
        打破;
    案例R.id.repeat_button:
// playSentence();
        打破;
    案例R.id.recordButton:
        如果(!录音){
        recordButton2.setBackgroundResource(android.R.drawable.button_onoff_indicator_on);
            记录= TRUE;
            recorder.reset();
            recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            recorder.setAudioEn codeR(MediaRecorder.AudioEn coder.AMR_NB);
            recorder.setOutputFile(outputFile.getAbsolutePath());
            尝试{
                。录音机prepare();
            }赶上(IllegalStateException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
            }赶上(IOException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
            }
            recorder.start();
        }
        否则,如果(录音){
                  recordButton2.setBackgroundResource(android.R.drawable.button_onoff_indicator_off);
            记录= FALSE;
            recorder.stop();
        }
        打破;
    案例R.id.playbackButton:
        Music.playSentence(这一点,OUTPUTFILE);
        打破;
    案例R.id.slowButton:
        如果(!slowedSpeech){
            slowButton2.setBackgroundResource(android.R.drawable.ic_dialog_alert);
            slowedSpeech =真;
 // slowspeech();
        }
        否则,如果(slowedSpeech){
            slowButton2.setBackgroundResource(android.R.drawable.ic_menu_recent_history);
            slowedSpeech = FALSE;
 // noSlowSpeech();
        }
        打破;
    }
}


解决方案

你是什么写文件的音轨是什么意思?你不需要写任何文件,音频轨道,可以设置音频源,该记录器将会创建音轨和从音频源读取PCM数据,然后连接codeR数据,将数据写入到输出文件

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;
    }
}

解决方案

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.

这篇关于Android的MediaRecorder到AudioTrack,录制和播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆