Skip to content Skip to sidebar Skip to footer

Nullpointerexception When Recording Is Stopped Android

I'm trying to make an app that starts recording when a button is clicked, and when is clicked again it stops recording. I don't think I have problems when I'm recording, but when I

Solution 1:

Initialize your media player in out side of startRecording()

myAudioRecorder = new MediaRecorder();

Modify your code like following

publicclassAudio {
privateString path;




MediaRecorder myAudioRecorder = newMediaRecorder();

publicAudio(String path) {
    this.path = path;
}

publicvoidplay() {

}

publicvoidstartRecording() {

    myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
    myAudioRecorder.setOutputFile(path);

    try {
        myAudioRecorder.prepare();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    myAudioRecorder.start();
}

publicvoidstopRecording() {
    myAudioRecorder.stop();
    myAudioRecorder.release();
    myAudioRecorder = null;
}
}

Hope this will help you

Solution 2:

The myAudioRecorder must be null at the time you call stopRecording(). I can not judge from your code why (do you call stopRecording() twice?). If you post more code I might be able to help you. You could also surround the statements in the stopRecording()method with a check:

publicvoidstopRecording() {
    if(myAudioRecorder != null){
       myAudioRecorder.stop();
       myAudioRecorder.release();
       myAudioRecorder = null;
    }
}

Solution 3:

privatevoidstopRecording() {
        if (null != recorder) {
            recorder.stop();
            recorder.reset();
            recorder.release();

            recorder = null;
        }
    }

Solution 4:

Solution 5:

I don't think you are taking the right approach. why declare media player as null in the beginning. initialise in first and when done use methods of media player class to release/reset it.

also before you start using mediaplayer use if(){...}block to check whether it is initialised. example below;

privatevoidstopRecording() {
      if (myAudioRecorder != null) {
        myAudioRecorder.stop();
        myAudioRecorder.reset();
        myAudioRecorder.release();

        myAudioRecorder = null;
     }
  }

Post a Comment for "Nullpointerexception When Recording Is Stopped Android"