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

public class Audio {
private String path;




MediaRecorder myAudioRecorder = new MediaRecorder();

public Audio(String path) {
    this.path = path;
}

public void play() {

}

public void startRecording() {

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

public void stopRecording() {
    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:

public void stopRecording() {
    if(myAudioRecorder != null){
       myAudioRecorder.stop();
       myAudioRecorder.release();
       myAudioRecorder = null;
    }
}

Solution 3:

private void stopRecording() {
        if (null != recorder) {
            recorder.stop();
            recorder.reset();
            recorder.release();

            recorder = null;
        }
    }

Solution 4:

please check if MediaRecorder object is not null before stopping it.


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;

   private void stopRecording() {
      if (myAudioRecorder != null) {
        myAudioRecorder.stop();
        myAudioRecorder.reset();
        myAudioRecorder.release();

        myAudioRecorder = null;
     }
  }

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