tyczj tyczj - 3 months ago 14
Android Question

MediaPlayer throwing IllegalStateException when calling onStop()

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

Answer

I guess you might be release your media player before executeing these lines. When i got this error i do like this below hope this solve your problem...

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