Question

MediaPlayer mediaPlayer = Common.getMediaPlayer();
mediaPlayer.reset();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(this,Uri.parse(url));
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
    @Override
    public void onPrepared(MediaPlayer mediaPlayer) {
        mediaPlayer.start();
    }
});
mediaPlayer.prepareAsync();

This is the code I have used. I click on some music I play it. If I click another music, it shuts off the old music and plays the new one clicked. However I am thinking if I can do, keep music 1 being played and once that one finishes play music 2.

Was it helpful?

Solution 2

Add oncomple listener , and have all your music in any array list. Keep rembering the current playing item. When one complete playing play the next in the list.

    mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
    @Override
    public void onCompletion(MediaPlayer mp) {
        // play next.
    }

OTHER TIPS

You'll probably want to maintain a list of songs and register an onCompletionListener to determine when each song finishes. In the onCompletion() callback, you'd pop the next song from the list and play it.

Since you'll eventually have several MediaPlayer listeners, it's probably best to avoid the variable access hassle that comes with anonymous inner classes and just have have your enclosing class implement them.

So you'll the completion listener with:

mediaPlayer.setOnCompletionListener(this);

And add implements OnCompletionListener to your class definition and add a method like the following:

@Override
public void onCompletion(MediaPlayer mp) {
    // Pop next song from the list, prepare it, and play it.
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top