Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to have MediaPlayer play one audio file and when it finishes play the next?

I want MediaPlayer to take in an arraylist of songTitles and have it check which song title came in, then play that song. When it finishes, I then want it to go to the next song title in the loop and play that one. However, my code only plays the last song.

public void play(Context c, ArrayList<String> songTitles) {
    stop();

    for (String song: songTitles){
        if (song.equalsIgnoreCase("shakeItOff")){
            mSongPlayer = MediaPlayer.create(c, R.raw.shaketoff);

        } else if (song.equalsIgnoreCase("dropItLow")){
            mSongPlayer = MediaPlayer.create(c, R.raw.dropitlow);

        } else if (song.equalsIgnoreCase("chachaslide")){
            mSongPlayer = MediaPlayer.create(c, R.raw.chachaslide);
        }

        mSongPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                if (mp == mSongPlayer) {
                    mSongPlayer.start();
                }
            }
        });

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


    }
}
like image 833
Sam321pbs Avatar asked Dec 31 '25 10:12

Sam321pbs


1 Answers

You need to use the MediaPlayer.OnCompletionListener to start the new track when the previous one finishes, so you could do something more like the following (I haven't compiled it, so there may be some syntax errors):

public void play(final Context c, ArrayList<String> songTitles) {
    stop();
    if (songTitles != null && songTitles.size > 0) {
        final List<String> playList = new ArrayList<String>(songTitles);

        String song = playList.get(0);
        playList.remove(0);

        mSongPlayer = MediaPlayer.create(c, getSongResourceId(song));

        mSongPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                if (mp == mSongPlayer) {
                    mSongPlayer.start();
                }
            }
        });

        mSongPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                stop();
                // Recursively call the play() method with one less
                // track in the list.
                play(c, playList);
            }
        });
    } 
} 

public int getSongResourceId(String songTitle) {
    if (song.equalsIgnoreCase("shakeItOff")){
        return R.raw.shaketoff;
    } else if (song.equalsIgnoreCase("dropItLow")){
        return R.raw.dropitlow;
    } else if (song.equalsIgnoreCase("chachaslide")){
        return R.raw.chachaslide;
    }
}

The first time through, this plays the first track in the list, then in the MediaPlayer.OnCompletionListener, it recursively calls the play() method with a copy of the list that has had the first track removed from it. This means each time play() is called, the list is shorter until we reach the end of the list.

like image 171
HexAndBugs Avatar answered Jan 02 '26 00:01

HexAndBugs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!