ateiob
ateiob

Reputation: 9106

How to identify which MP3 file ended in MediaPlayer.OnCompletionListener?

In my ativity's onCreate(), I set a MediaPlayer.OnCompletionListener, then play an MP3 file:

    MediaPlayer p = MediaPlayer.create(this, R.raw.intro);
    p.setOnCompletionListener(this);
    p.start();      

And when playing ends, I just handle this event in:

    public void onCompletion(MediaPlayer mp) {
      // handle completion
    }

All nice and dandy but now I want to play two different MP3 files and handle completion differently based on which file was played.

Is there a way to tell from the MediaPlayer parameter which piece ended?

Upvotes: 9

Views: 1386

Answers (2)

mozillanerd
mozillanerd

Reputation: 560

The OnSetCompletionListener triggered identifies which MediaPlayer completed. As to the mp3 file, your data model should be expressed as a list of MediaPlayer objects (objects may be create from subclass of MediaPlayer) which should know the mp3 file they are playing or completed playing. See http://davanum.wordpress.com/2007/12/29/android-videomusic-player-sample-from-local-disk-as-well-as-remote-urls/ for example As to the model create a class that inherits from MediaPlayer. In that new class maintain the mp3 file name - like 'fn'. So then p.fn gives you the file for the mp3

Upvotes: 2

edthethird
edthethird

Reputation: 6263

The callback public void onCompletion(MediaPlayer mp) gives you a reference to the MediaPlayer.

public void onCompletion(MediaPlayer mp) {
    if (mp.equals(p){
        //do action for media player p

    } else if (mp.equals(q)){
        //do action for media player q
    }
}

Upvotes: 4

Related Questions