Search code examples
androidsmart-playlist

Android media player playlist and reset


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.


Solution

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