Android中的几个录音

时间:2012-11-27 12:26:03

标签: android android-service audio-recording

我有一个Android代码,应该录制几个音频。

第一张录音效果很好,我点击了记录,而不是停止,这一切都还可以!但如果我点击记录中的另一个时间我就会崩溃!我做错了什么?

btnEnregistrement = (ImageButton) findViewById(R.id.button_lancer_enregistrement);
        btnEnregistrement.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(btnEnregistrement.getTag().equals("false")){
                        btnEnregistrement.setBackgroundDrawable(getResources().getDrawable(R.drawable.rec_actionaudio_xml));
                        btnEnregistrement.setTag("true");

                        try {
                            recorder.start();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                }else{
                        btnValider.setBackgroundDrawable(getResources().getDrawable(R.drawable.valider_cliquable_xml));

                        btnEnregistrement.setBackgroundDrawable(getResources().getDrawable(R.drawable.rec_nonaction_xml));
                        btnEnregistrement.setTag("false");

                        try {
                            recorder.stop();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }       


}               
        }

AudioRecorder Class

import java.io.File;
import java.io.IOException;

import android.media.MediaRecorder;
import android.os.Environment;


public class AudioRecorder {

    final MediaRecorder recorder = new MediaRecorder();
    final String path;

    /**
    * Creates a new audio recording at the given path (relative to root of SD card).
    */
    public AudioRecorder(String path) {
        this.path = sanitizePath(path);
    }

    private String sanitizePath(String path) {
        if (!path.startsWith("/")) {
            path = "/" + path;
        }
        if (!path.contains(".")) {
            path += ".3gp";
        }   
        return Environment.getExternalStorageDirectory().getAbsolutePath() + path;
    }

    /**
     * Starts a new recording.
     */
    public void start() throws IOException {
        String state = android.os.Environment.getExternalStorageState();
        if(!state.equals(android.os.Environment.MEDIA_MOUNTED))  {
            throw new IOException("SD Card is not mounted.  It is " + state + ".");
        }

        // make sure the directory we plan to store the recording in exists
        File directory = new File(path).getParentFile();
        if (!directory.exists() && !directory.mkdirs()) {
            throw new IOException("Path to file could not be created.");
        }

        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        recorder.setOutputFile(path);
        recorder.prepare();
        recorder.start();
    }

    /**
    * Stops a recording that has been previously started.
    */
    public void stop() throws IOException {
        recorder.stop();
        recorder.release();
    }

}

0 个答案:

没有答案