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?
OnCompletionListener. Interface definition for a callback to be invoked when playback of a media source has completed.
android.media.MediaPlayer. MediaPlayer class can be used to control playback of audio/video files and streams. MediaPlayer is not thread-safe. Creation of and all access to player instances should be on the same thread. If registering callbacks, the thread must have a Looper.
Advertisements. Android provides many ways to control playback of audio/video files and streams. One of this way is through a class called MediaPlayer.
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
}
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With