3

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();
            }
        });


    }
}

2 Answers 2

3

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.

3
  • 1
    @xiumeteo, I assume you mean because a new MediaPlayer is created each time? There shouldn't be too much impact on performance, but if you want to prevent the reallocation and garbage collection associated with it, you could easily change it to do the MediaPlayer.create() only if mSongPlayer is null, and otherwise do mSongPlayer.reset(); mSongPlayer.setDataSource(); mSongPlayer.prepare(); mSongPlayer.start(); although you'd have to convert the resource ids as setDataSource() doesn't take a resource id ...
    – HexAndBugs
    Jul 16, 2015 at 15:53
  • ... You could also avoid the copying of the ArrayList and just iterate over it instead of using recursion if you maintained a counter of the position in the ArrayList and incremented it in the onCompletion() method until you'd reached songTitles.size(). These would be fairly easy changes to make if you wanted to. The main point is you need to start the new song playing in the OnCompletionListener each time the previous song has finished. These changes would be easy to make, but I'd only expect them to have a minor performance improvement. Of course, that could be important to you.
    – HexAndBugs
    Jul 16, 2015 at 15:55
  • Shouldn't it be mSongPlayer.stop(); on line 2?
    – Zypps987
    Nov 19, 2016 at 23:51
0

I'm not sure if there is a way to do it only using a single MediaPlayer. But, I'm sure that your are only playing the last song due to the follow:

  1. Every time you create the MediaPlayer, you are overriding the previously one, so if your first MediaPlayer is for "shakeItOff", the next is for "dropItDown", when you reach your last one, the "chachaslide", you end up referencing a MediaPlayer that will play chachaslide.
  2. Every time you go through the loop you are overriding the last OnCompletionListener that you attached before. So, you end with the last listener that you attached (your last song)
  3. You are asserting the prepared MediaPlayer to the current MediaPlayer, so even if you manage to prepare every MediaPlayer to play, you will only play the currentOne, as for 1 is "chachaslide".

You can check the lifecycle of the MediaPlayer here: http://developer.android.com/reference/android/media/MediaPlayer.html

So, the only way you can reuse the same MediaPlayer reference is to manage it for yourself, other way you end with at least 3 instances at memory of the MediaPlayer

1
  • Thank makes a lot of sense. Thank you!
    – Sam321pbs
    Jul 15, 2015 at 1:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.