MediaRecorder crashes on start

余生颓废 提交于 2019-11-27 15:09:32

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

Try putting the start function in the same block as prepare function. maybe there's an exception blocking prepare from executing and goes directly to start thus causing an IllegalStateException.

    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(output_formats[currentFormat]);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    recorder.setOutputFile(getFilename());


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

i am using the following code,works perfectly for me..

protected void startRecording() {
    // TODO Auto-generated method stub
    i++;
     mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
     mFileName += "/audiorecordtest"+i+".3gp";
    recorder = new MediaRecorder();

    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    recorder.setOutputFile(mFileName);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

      try {
        recorder.prepare();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        Toast.makeText(getApplicationContext(), "IllegalStateException called", Toast.LENGTH_LONG).show();


    } catch (IOException e) {
        // TODO Auto-generated catch block
        Toast.makeText(getApplicationContext(), "prepare() failed", Toast.LENGTH_LONG).show();

    }

      recorder.start();
}

 private void stopRecording() {
     recorder.stop();
     recorder.release();
     recorder = null;
    }

These methods must arrange the order it will run. Here:

recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setOutputFile(mFileName);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

Try to start your recorder only when it is prepared :

    try {
        recorder.prepare();
        recorder.start();
        mStartRecording = true;
    }  catch (IOException e) {
        Log.e( LOG_TAG, "Error when preparing or starting recorder", e);
    }

I had the same problem. This is because I had missed a slash while setting the filename for the recorded audio.

change

this.fileName = Environment.getExternalStorageDirectory().getAbsolutePath();
this.fileName += "yourfilename.3gp";

to

this.fileName = Environment.getExternalStorageDirectory().getAbsolutePath();
this.fileName += "/yourfilename.3gp";

I had same problem because of my SurfaceView has been made invisible. So make it Visible

  mSurfaceView.setVisibility(View.VISIBLE);

Must first call setOutputFile() , and then call other methods.

and you must create file before all.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!