Skip to content Skip to sidebar Skip to footer

Android Mediaplayer Prepareasync Method

I have a strange problem. I use the prepareAsync method with the MediaPlayer, but the listener that I declared just never gets fired. I try to stream a live .mp3 feed from the Inte

Solution 1:

You are setting the OnPreparedListener in your Activity's create method and later on, possibly, creating a new MediaPlayer in your preparePlayer() method. Any newly created MediaPlayer's wont have the prepareListener set. You should also add an errorlistener to find out whats going on.

You may want to move the setOnPreparedListener call inside the

if (mediaPlayer == null) {
    mediaPlayer = new MediaPlayer();
}

as well as setting a setOnErrorListener like so:

if (mediaPlayer == null) {
    mediaPlayer = newMediaPlayer();
    mediaPlayer.setOnErrorListener(....);
    mediaPlayer.setOnPreparedListener(newOnPreparedListener() {
        publicvoidonPrepared(MediaPlayer mp) {
            ToggleButtonplayButton= (ToggleButton) findViewById(R.id.playToggleButton);
            playButton.setClickable(true);
            mp.start();
        }
    });
}

Please change e.printStackTrace(); to

Log.e("tag", e.getMessage(), e);  

Hope this helps, let us know if you have any log data as this will also be useful.

Solution 2:

Turned out, I forgot to mention the "@Override" on top of my inner method. Like this :

mediaPlayer.setOnPreparedListener(newOnPreparedListener() {
    @OverridepublicvoidonPrepared(MediaPlayer mp) {
        ToggleButtonplayButton= (ToggleButton) findViewById(R.id.playToggleButton);
        playButton.setClickable(true);
        mp.start();
    }
});

Now everything works fine. Well... almost, since it takes forever to buffer...

Post a Comment for "Android Mediaplayer Prepareasync Method"