Search code examples
androidmedia-playerandroid-mediaplayerillegalstateexception

IllegalStateException in Android MediaPlayer execution


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

Solution

  • 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.