Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android media player returns IllegalStateException

I have following code to play small audio files

private void playVoice() {

     if (mPlayVoice != null) {
         if (mPlayVoice.isPlaying()) {
             mPlayVoice.release();
             mPlayVoice = null;
         }
     }
     mPlayVoice = 
         MediaPlayer.create(BirdsActivity.this, mSoundIds[getCurrentIndex()]);
     mPlayVoice.start();
 }

It works fine in Samsung galaxy tab but gives below error in small device(I Checked in Sony xperia mini pro my project)

08-17 12:45:45.232: ERROR/AndroidRuntime(6639): java.lang.IllegalStateException
    08-17 12:45:45.232: ERROR/AndroidRuntime(6639):     at   android.media.MediaPlayer.isPlaying(Native Method)
    08-17 12:45:45.232: ERROR/AndroidRuntime(6639):     at           com.android.mds.kidsapps.alphakids.BirdsActivity.playVoice(BirdsActivity.java:146)
like image 239
vnshetty Avatar asked Aug 17 '11 07:08

vnshetty


3 Answers

You're doing this:

 PlayVoice.release(); 

Do you not mean

 mPlayVoice.release(); 

If you have other issues this is the best document to consult:

Android MediaPlayer

EDIT

Ok if you are here: isPlaying() Invalid States it show's you're trying to call isPlaying() while the player is in the error state. So you need to work out why it is already in the error state.

In general, some playback control operation may fail due to various reasons, such as unsupported audio/video format, poorly interleaved audio/video, resolution too high, streaming timeout, and the like.

Have a look at adding an error listener: setOnErrorListener()

like image 173
Blundell Avatar answered Nov 17 '22 21:11

Blundell


Use the following code as i was facing the same exception.

try {
    if(mPlayVoice!=null && mPlayVoice.isPlaying()) {
        Log.d("TAG------->", "player is running");
        mPlayVoice.stop();
        Log.d("Tag------->", "player is stopped");
        mPlayVoice.release();
        Log.d("TAG------->", "player is released");
    }
} catch(Exception e){
}

Here write whatever you want to do. Actually the condition checking like isPlaying() or checking for null generates the IllegalStateException.....

like image 29
picaso Avatar answered Nov 17 '22 21:11

picaso


You may have to clear the audioGroup joined with audioStream. Mine worked with the following code:

public static void audioPlayCaptureStop()
        {

            try 
            {
                 if(audioStream.isBusy()) 
                 {
                     audioGroup.clear();
                     audioStream.release();
                     System.out.println("audioStream released");
                 }

            } catch (Exception e) {
                System.out.println("audioStream release exception: "+e.toString());
            }
        }
like image 2
ani0904071 Avatar answered Nov 17 '22 19:11

ani0904071