MediaRecorder crashes on start

后端 未结 8 1769
没有蜡笔的小新
没有蜡笔的小新 2020-12-06 12:25

i\'ve searched many topics but no straight answer.

I have this code :

        recorder = new MediaRecorder();
    recorder.setAudioSource(MediaRecord         


        
8条回答
  •  鱼传尺愫
    2020-12-06 12:34

    Ok, I got it. I guess you initialized mStartRecording to true.

    Thus your if is going into the else block. In it, you stop a brand new instance of MediaRecorder and the state diagram doesn't allow that.

    Make your media recorder a field of your class. And initialize properly your mStartRecording boolean variable to false. Re instanciate your media recorder only if your field is null.

    if( recorder == null ) {
       recorder = new MediaRecorder();
       recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
       recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    
       recorder.setOutputFile(mFileName);
       recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    }//if
    if(!mStartRecording) {
        btn.setText("Stop Recording");
        try {
            recorder.prepare();
            recorder.start();
            mStartRecording = true;
        }  catch (IOException e) {
            e.printStackTrace();
        }//catch
    } else {
        btn.setText("Start Recording");
        mStartRecording = false;
        recorder.stop();
        recorder.reset();
        recorder.release();
        recorder = null;
    }//else
    

提交回复
热议问题