Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple MediaPlayers do not work on Nexus 5

I am building an app that uses two media players to play two audio files at the same time. This works fine on my Samsung Galaxy S3 device, but when I run it on a Nexus 5 the audio becomes fragmented / un-listenable.

I am wondering wether using two media players concurrently is possible on the Nexus 5, and if not how can I play two audio files at the same time?

like image 946
Oliver Budiardjo Avatar asked Feb 19 '14 22:02

Oliver Budiardjo


2 Answers

The SoundPool class is specifically designed with multiplexing multiple audio files together so that they can be played together.

SoundPool soundPool = new SoundPool(2, // number of streams
                                    AudioManager.STREAM_MUSIC, // stream type
                                    0); // source quality, does nothing
soundPool.setOnLoadCompleteListener(loadCompleteListener);
int soundOneId = soundPool.load(context, R.raw.sound1, 1);
int soundTwoId = soundPool.load(context, R.raw.sound2, 1);
// Once loadCompleteListener.onLoadComplete has been called for both sounds
soundPool.play(soundOneId);
soundPool.play(soundTwoId);
like image 177
ianhanniballake Avatar answered Sep 27 '22 17:09

ianhanniballake


Use AudioTrack, which provides low latency audio even when streaming. http://developer.android.com/reference/android/media/AudioTrack.html.

like image 42
Dave C Avatar answered Sep 27 '22 17:09

Dave C