Leo Galindo
Leo Galindo

Reputation: 308

Android Media player OncompletionListener Play Next song but not the second song

I have an App with a music Player, i am using the @override Method OnCompletionListener to listen when the music is complete and pass to the next song, the bellow code works, but in the second song when finish OncompletionListener is not calling Again and the music stop when finish, how pass to the next song continually?

OnCompletionListener

mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                          @Override
                          public void onCompletion(MediaPlayer mp) {
                              NextSong();
                          }
                      });

NextSong Method

public void NextSong (){

    num = posicion++;

    if (MainActivity.num <= MainActivity.listaArray.size() - 2) {

        mediaPlayer.reset();
        uri = Uri.parse(String.valueOf(archivosMp3[num + 1]));
        mmr.setDataSource(String.valueOf(archivosMp3[num + 1]));  

        mediaPlayer = mediaPlayer.create(Servicio_Reproductor.this, MainActivity.uri);
        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {

                mediaPlayer.start();
            }
        });

Upvotes: 0

Views: 3133

Answers (1)

Komal12
Komal12

Reputation: 3348

Try this,

    public class MediaplayerActivity extends Activity implements OnCompletionListener 
    {

        @Override
        protected void onCreate(Bundle savedInstanceState) {

        mMediaPlayer.setOnCompletionListener(this);
        }

        @Override
        public void onCompletion(MediaPlayer mp) {

        .
        .
        .
         mediaPlayer = mediaPlayer.create(Servicio_Reproductor.this, MainActivity.uri);
            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mMediaPlayer.setOnCompletionListener(this);
                    mediaPlayer.start();
                }
            });

        }
    }

Upvotes: 1

Related Questions