Reputation: 83
I'm creating an android application that I want it to plays multiple mp3 files one after one.
I've got an mediaPlayer on my app, how do I set a queue to play mp3 files?
Upvotes: 4
Views: 2552
Reputation: 1
like this:
private void PlaySoundNv(int soundId){
if(mediaPlayer ==null || !mediaPlayer.isPlaying()){
mediaPlayer = MediaPlayer.create(this,soundId);
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
mediaPlayer.reset();
if (!qSoundID.isEmpty()){
PlaySoundNv(qSoundID.poll());
}
}
});
mediaPlayer.start();
}else{
qSoundID.add(soundId);
}
}
Upvotes: 0
Reputation: 1577
I added a que which I'm using as a FIFO:
companion object { val playList: MutableList = Collections.synchronizedList(mutableListOf()) }
I have a function that initializes media player if necessary, then adds the mri file to the que. After queing the file, I call a start function that exits is the player is playing and starts the next file otherwise. I configured mediaPlayer with an OnCompletionListener which starts the next file if the que isn't empty. It's simple and hasn't given me any problems.
Upvotes: 0
Reputation: 951
You can set up a LinkedList and a complementary ListIterator to manage a queue of MediaPlayers.
For each mp3 files that you know should be in the queue, call the ListIterator's add()
method with a media player created withMediaPlayer.create();
Be warned that you need to be very careful about where the iterator position is while managing the queue.
Upvotes: 0
Reputation: 2017
Way to achieve this is to create a class that implements OnCompletionListener which handles the onCompletion and receives the next file to play. First define your Class to PlayMedia like this:
import android.content.Context;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.AsyncTask;
import android.util.Log;
public class PlayMedia extends AsyncTask<Void, Void, Void> {
private static final String LOG_TAG = PlayMedia.class.getSimpleName();
Context context;
private MediaPlayer mediaPlayer;
int[] soundIDs;
int idx =1;
public PlayMedia(MediaPlayer mediaPlayer) {
this.mediaPlayer = mediaPlayer;
}
public PlayMedia(final Context context, final int[] soundIDs) {
this.context = context;
this.soundIDs=soundIDs;
mediaPlayer = MediaPlayer.create(context,soundIDs[0]);
setNextMediaForMediaPlayer(mediaPlayer);
}
public void setNextMediaForMediaPlayer(MediaPlayer player){
player.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
if(soundIDs.length>idx){
mp.release();
mp = MediaPlayer.create(context,soundIDs[idx]);
setNextMediaForMediaPlayer(mp);
mp.start();
idx+=1;
}
}
});
}
@Override
protected Void doInBackground(Void... params) {
try {
mediaPlayer.start();
} catch (IllegalArgumentException e) {
Log.e(LOG_TAG, "", e);
} catch (SecurityException e) {
Log.e(LOG_TAG, "", e);
} catch (IllegalStateException e) {
Log.e(LOG_TAG, "", e);
}
return null;
}
}
Then use PlayMedia like this:
int[] soundIDs = {R.raw.firstmp3, R.raw.secondmp3};
PlayMedia playAudio = new PlayMedia(context,soundIDs);
playAudio.execute();
Upvotes: 5