MediaPlayer throwing IllegalStateException when calling onStop()

android, android-mediaplayer

Solution

I guess you might be nulling your instance before executing these lines. When I got this error I check for null first.

if (mp != null) {
    try {
        mp.stop(); //error
        mp.reset();
        mp.release();
    } catch(Exception e){
        Log.d("Nitif Activity", e.toString());
    }
}

Problem

I have an `AlertDialog`, which stops playing a sound when I have clicked, but on some devices it appears that calling `onStop()` throws an `IllegalStateException`, but why? If the dialog is up, that means the sound is playing, so it should be a case where the audio is not playing. I surrounded it with a try catch for now, but what would cause this? ``` alert.setPositiveButton("YES", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { try{ mp.stop(); //error mp.reset(); mp.release(); }catch(Exception e){ Log.d("Nitif Activity", e.toString()); } v.cancel(); popupMessage(); finish(); } }); ```

Original source