9

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?

4 Answers4

5

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);
ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • 1
    im working with larger audiofiles (like a whole song or an audiobook). Im testing the code you posted and the soundPoolOnLoadCompleteListener never calls onLoadComplete – Oliver Budiardjo Feb 19 '14 at 23:17
  • You're playing two longer files concurrently? Or just one longer file and one short sound? – ianhanniballake Feb 19 '14 at 23:23
  • two long files concurrently. Tested again, onLoadComplete gets called after a long wait. Now they play together nicely, but both of them cut out after only a few seconds; the problem being that i need loading to happen quickly and I need the whole thing to play – Oliver Budiardjo Feb 19 '14 at 23:33
  • Yeah SoundPool is designed primarily for smaller length clips (as all the clips are decompressed and stored in memory for extremely quick starting times), rather than extremely long audio files. – ianhanniballake Feb 19 '14 at 23:44
5

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

Dave C
  • 928
  • 6
  • 12
3

The tutorial here provides a complete example on how to use AudioTrack to play multiple audios simultaneously.

Note that playback using AudioTrack would still fail if no multiple instances of the same codec are supported on Nexus 5 and other devices with the same limitation, so your app may not work on all devices.

Kai
  • 15,284
  • 6
  • 51
  • 82
  • it does work but for few sec wav files i guess, can you work on a sample that uses longer wav files and works fine ?. Currently it appears to be playing sound stream very slowly like at 0.1 x speed and lot of garbage sounds. – Shubhank Sep 06 '14 at 18:50
  • I think it means that AudioTrack doesn't work for N5 neither. In this case going native with OpenSL ES (Android 4.1+) or doing your own mixing with raw PCM data may be the way to go. Mixing raw PCM: http://stackoverflow.com/questions/16766248/mix-audio-in-android, mixing with OpenSL ES: [Simple streaming audio mixer for Android with OpenSL ES - part 1](http://sbcgamesdev.blogspot.tw/2013/01/simple-streaming-audio-mixer-for.html), [part 2](http://sbcgamesdev.blogspot.tw/2013/02/simple-streaming-audio-mixer-for.html) – Kai Sep 07 '14 at 01:17
1

I have the same problem, it's a total nightmare:

http://forum.openframeworks.cc/t/choppy-audio-with-ofxandroidsoundplayer-mediaplayer/20961/5

The only solution seems to use something else to play back multi-track audio, be it FMOD or ExoPlayer or the hand-rolled WAV mixer mentioned above.