Playing Multiple Songs With Mediaplayer At The Same Time: Only One Is Really Playing
Solution 1:
I achieved what you're looking for using a CyclicBarrier instance and an inner class implementation.
Example:
publicenumMP_COMMAND {
START,
STOP,
PAUSE
}
/**
* Uses threads to execute synced commands for the current video media player and
* background music player in tandem.
*/publicvoidsyncedCommand(MediaPlayer player1, MediaPlayer player2, MP_COMMAND command){
final CyclicBarrier commandBarrier = newCyclicBarrier(2);
newThread(newSyncedCommandService(commandBarrier, player1, command)).start();
newThread(newSyncedCommandService(commandBarrier, player2, command)).start();
}
/**
* Inner class that starts a given media player synchronously
* with other threads utilizing SyncedStartService
*/privateclassSyncedCommandService implements Runnable {
privatefinal CyclicBarrier mCommandBarrier;
private MediaPlayerTest.MP_COMMAND mCommand;
private MediaPlayer mMediaPlayer;
publicSyncedCommandService(CyclicBarrier barrier, MediaPlayer player, MediaPlayerTest.MP_COMMAND command){
mCommandBarrier = barrier;
mMediaPlayer = player;
mCommand = command;
}
@Override publicvoidrun(){
try {
mCommandBarrier.await();
} catch (InterruptedException | BrokenBarrierException e) {
e.printStackTrace();
}
switch (mCommand) {
case START:
mMediaPlayer.start();
break;
case STOP:
mMediaPlayer.stop();
break;
case PAUSE:
mMediaPlayer.pause();
break;
default:
break;
}
}
}
You'd utilize it like so:
syncedCommand(mCurrentVideoPlayer, mBackgroundMusic, MP_COMMAND.START);
If you had the requirement that it be usable for any number of media players, you could easily implement it - my requirements just call for two.
I realize this question is old, but this page is where I found myself while searching for a solution, so I hope this helps anyone stuck on this issue in the future.
Solution 2:
Well, I believe I found what I could name "a temporary solution".
MediaPlayertrack1= MediaPlayer.create(this, R.raw.track1);
track1.start();
MediaPlayertrack2= MediaPlayer.create(this, R.raw.track2);
track2.start();
MediaPlayertrack3= MediaPlayer.create(this, R.raw.track3);
track3.start();
The problem with this is that the create()
method creates a noticeable gap when the songs are being played.
So this is not it.
After a while, I tried the following :
MediaPlayertrack1= MediaPlayer.create(this, R.raw.track1);
track1.start();
track1.pause();
MediaPlayertrack2= MediaPlayer.create(this, R.raw.track2);
track2.start();
track2.pause();
MediaPlayertrack3= MediaPlayer.create(this, R.raw.track3);
track3.start();
track3.pause();
// resuming...
track1.start();
track2.start();
track3.start();
That whay, the songs are more synchronous. I can still hear a very small gap but it is way better. I found this solution quite strange as well as ugly.
Unless someone has another idea, I will stick with that solution.
Solution 3:
As Andrey Nikishaev already wrote in a comment, it seems like some devices have problems using multiple MediaPlayers at the same time. While only one was playing on my Nexus 5, it worked on my Moto G4 (but still having short breaks during playback). Both running with Android 6.0.1.
I suggest to use the ExoPlayer for this use-case instead. It worked fine for me, at least on my two devices.
Solution 4:
I converted my mp3 in ogg file audio (using Audacity) and I am been able to use 2 Mediaplayer istances and listen the audio mixed.
I didn't try is it is possible for more than 3.
Solution 5:
hi try below code it will work it the song wont stop you just need to select the song from spinner and click on start button, then select next song from spinner and click start it will work.
publicclassMainActivityextendsAppCompatActivity {
Spinner sp;
Button bstart;
MediaPlayer mp;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sp=(Spinner)findViewById(R.id.sp);
String [] mys={"demo","democheap","demorehana"};
ArrayAdapter<String> data=newArrayAdapter<String>(this,R.layout.support_simple_spinner_dropdown_item,mys);
sp.setAdapter(data);
}
publicvoidcode(View v)
{
if(sp.getSelectedItem().toString().equals("demo"))
{
mp=MediaPlayer.create(this,R.raw.demo);
}
elseif(sp.getSelectedItem().toString().equals("democheap"))
{
mp=MediaPlayer.create(this,R.raw.democheap);
}
elseif(sp.getSelectedItem().toString().equals("demorehana"))
{
mp=MediaPlayer.create(this,R.raw.demorehana);
}
if(v.getId()==R.id.bstart)
{
mp.start();
}
}
}
Post a Comment for "Playing Multiple Songs With Mediaplayer At The Same Time: Only One Is Really Playing"