0

I'm trying to run beeb sound continuously until user pressed the button. I added alarm.mp3 file to raw folder. But this media file not executing. I'm getting IllegalStateException.

This is my code:

MediaPlayer mp = new MediaPlayer(); try{ mp.release(); mp = MediaPlayer.create(this,R.raw.alarm); mp.prepare(); mp.setVolume(1f, 1f); mp.setLooping(true); mp.start(); }catch(IllegalStateException e){ System.out.println("Test Exception "+e); }catch (IOException e) { // TODO: handle exception System.out.println("Test Exception "+e); } 

I added that mp3 file to asset folder and try to play it but still I'm getting same exception.

This is the code I used:

MediaPlayer mp = new MediaPlayer(); try{ mp.release(); AssetFileDescriptor afd=context.getAssets().openFd("alarm.mp3s"); mp.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength()); mp.prepare(); mp.setVolume(1f, 1f); mp.setLooping(true); mp.start(); }catch(IllegalStateException e){ System.out.println("Test Exception "+e); }catch (IOException e) { // TODO: handle exception System.out.println("Test Exception "+e); } 

Any help would be appriciated. If you need more information please let me know.

UPDATE:

try{ mp = MediaPlayer.create(context,R.raw.alarm); mp.setVolume(1f, 1f); mp.setLooping(true); mp.start(); }catch(IllegalStateException e){ System.out.println("Test Exception "+e); } 

When user click the button:

mp.stop(); if(!mp.isPlaying()){ mp.release(); } 

This is the exception:

10-04 12:50:06.105: I/System.out(14074): Test Exception java.lang.IllegalStateException 
2
  • Once release() has been called MediaPlayer cannot be used. Call the method when you're completely done with MediaPlayer and its resources are no longer needed. Commented Oct 4, 2016 at 2:29
  • log and post the full stacktrace, not just that short line. Commented Oct 4, 2016 at 2:58

1 Answer 1

1

Without seeing stacktrace, it´s just an assumption. But you are directly calling

mp.release(); 

just after you have created one. The second is you are creating the mediaPlayer with new MediaPlayer() AND with create(). You don´t need new MediaPlayer() if you use create() and with mp.create() method, you don´t need to call prepare(). I would try it like this:

 mp = MediaPlayer.create(this,R.raw.alarm); mp.setVolume(1f, 1f); mp.setLooping(true); mp.start(); 

And just call release if mediaPlayer is finished. Like described in the API, after release(), the MediaPlayer is in the end state:

Once the MediaPlayer object is in the End state, it can no longer be used and there is no way to bring it back to any other state.

You should follow the API, I know it´s much to read, but it´s very important to know all the stuff about the MediaPlayer.

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks for your support. I did changes you suggested but still I'm getting same Exception.
then please edit the posted code and please post your stacktrace and point out the line where the error occurs.
I updated the question.It is difficult to get the whole stack trace I'm working on a big project and couldn't find relevant part.
Ok, but it´s very important to see the relevant part of the stacktrace because here we can see the cause of the error and the part where it happens. From your updated code, it seems to be ok. So there must be any wrong call between...
and you should clean your project and deinstall it from your device. That is also often a problem. old version stays on the device and it´s not correctly updated.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.