Palaniraja
Palaniraja

Reputation: 233

Android mediaplayer play a Songs as orderly

I am new to Android. In my app MediaPlayer plays groups of songs in order. The songs are stored in one arraylist and the song names are stored in another arraylist..

for(i=0;i< songarray.sizeof();i++)
{
  mp=MediaPlayer.create(this,(Integer) songarray.get(i));

  sname.setText(songname.get(i));
  mp.start
}

The problem is that the songs play only one then print one song name.

Upvotes: 1

Views: 1446

Answers (1)

MByD
MByD

Reputation: 137442

You should set a listener to the end of the song, and then start the next one (BTW - what's sizeof()):

int index = 0;
sname.setText(songname.get(i));
mp = MediaPlayer.create(this,(Integer) songarray.get(i));
mp.start(); //as per @iturki comment
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
    @Override
    public void onCompletion(MediaPlayer player) {
        mp.release();
        if (i < (songarray.size() - 1)) {
            i++;
            mp = MediaPlayer.create(this,(Integer) songarray.get(i));
            sname.setText(songname.get(i));
            mp.setOnCompletionListener(this);
            mp.start();            
        }           
    }
});

Upvotes: 2

Related Questions