Skip to content Skip to sidebar Skip to footer

Mediaplayer.prepare Is Throwing An Illegalstateexception When Playing M4a File

I have a list of songs that I'm streaming using the MediaPlayer. Some of the songs consistently work and others consistently do not work. I can't see a difference between these fil

Solution 1:

Thanks MisterSquonk I'm sure that way would work.

In my particular case after beating my head against the wall for a while I realized that on some songs, I was getting to the buffered amount before the player state was getting set to prepared. So I added a check to make sure that the MediaPlayer was in the "PREPARED" state and then it worked great:

// Media prepared listener
    mediaPlayer.setOnPreparedListener(
            newMediaPlayer.OnPreparedListener() {
                publicvoidonPrepared(MediaPlayer mp) {
                    setPlayerState(PlayerState.PREPARED);
                }
            });

    // Media buffer listener
    mediaPlayer.setOnBufferingUpdateListener(
            newMediaPlayer.OnBufferingUpdateListener() {
                publicvoidonBufferingUpdate(MediaPlayer mp, int percent) {

                    // Sometimes the song will finish playing before the 100% loaded in has been// dispatched, which result in the song playing again, so check to see if the // song has completed firstif(getPlayerState() == PlayerState.COMPLETED)
                        return;

                    if(getPlayerState() == PlayerState.PAUSED)
                        return;

                    // If the music isn't already playing, and the buffer has been reachedif(!mediaPlayer.isPlaying() && percent > PERCENT_BUFFER) {
                        if(getPlayerState() == PlayerState.PREPARED)
                        {
                            mediaPlayer.start();
                            setPlayerState(PlayerState.PLAYING);
                        }
                        //if it isn't prepared, then we'll wait till the next buffering//updatereturn;
                    }
                }
            });

Solution 2:

OK, I hacked together a minimal Mediaplayer implementation in a 'sandbox' app/activity I always keep spare for testing.

I might be wrong but if you're streaming these songs over the net, you'll need to prefix the url with http://.

I tried the urls with Winamp and Chrome verbatim (no protocol prefix string) and they worked fine although it's likely both of those applications will use some form of intelligence to work out how to connect/stream.

If I tried that in my mediaPlayer code, I get the same exception as you but if I prefix the urls with http:// the songs play fine.

Example...

// Activity scopeButton button;
CheckBox checkBox;
String url = "";

publicvoidonCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //button declared in my activity
    button = (Button)findViewById(R.id.button);
    button.setOnClickListener(this);

    if (!checkBox.isChecked())
        url = getString(R.string.url_song1);
    else
        url = getString(R.string.url_song2);

    mediaPlayer = newMediaPlayer();
}

@OverridepublicvoidonClick(View arg0) {
    try {
        Log.i(TAG, "onClick() entered...");
        mediaPlayer.setDataSource(url);
        Log.i(TAG, "Preparing mediaplayer...");
        mediaPlayer.prepare();
        Log.i(TAG, "Starting mediaplayer...");
        mediaPlayer.start();
    } catch (Exception e) {
        e.printStackTrace();
        Log.e(TAG, "bad stream");
    }       
}

If I copy the songs to my SD card both play fine and as long as the internet url strings have an 'http://' prefix then they also work.

Post a Comment for "Mediaplayer.prepare Is Throwing An Illegalstateexception When Playing M4a File"