Is it possible for MediaPlayer to play one audio file and when it ends the game next?

I want the MediaPlayer to pick up the arraylist from the songTitles and ask it to check which song title is in, then play that song. When it ends, I want it to go to the next song title in the loop and play it back. 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();
            }
        });


    }
}

      

+3


source to share


2 answers


You need to use MediaPlayer.OnCompletionListener

to start a new track when the previous one ends, so you can do something more similar to the following (I didn't compile it, so there might 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 it plays the first track in the list, then MediaPlayer.OnCompletionListener

it recursively calls the method play()

with a copy of the list from which the first track was deleted. This means that every time it is called play()

, the list is shorter until we get to the end of the list.

+3


source


I'm not sure if there is a way to do this using just one MediaPlayer. But, I'm sure you are only playing the last song because of the following:

  • Every time you create a MediaPlayer, you override the previous one, so if your first MediaPlayer is for "shakeItOff", the next one is for "dropItDown", when you reach your last, "chachaslide", you end up to reference the MediaPlayer, which will play chachaslide ...
  • Every time you go through the loop, you are overriding the last OnCompletionListener component that you previously used. So you end up with the last listener you attached (your last song)
  • You are asserting the prepared MediaPlayer to the current MediaPlayer, so even if you manage to prepare each MediaPlayer, you will only play currentOne, since for 1 it is "chachaslide".


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

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

0


source







All Articles