Reputation: 16825
How can I play background audio, in Android, without interrupting the MediaPlayer
playback, by either using MediaPlayer
(preferred) or OpenSL ES?
I know SoundPool
is able to play sound effects without interrupting any MediaPlayer
playback, but the size is limited to 1M per effect, which is way to less. Not requesting audio focus, via AudioManager
doesn't seem to work either, audio doesn't play at all in this case.
And in the case of OpenSL ES, all audio generally stops when I start to play a longer asset file. It's similar to the behaviour of SoundPool
described above.
Edit from the comments:
I don't want to interrupt other music players, it's the background audio of a game, which shall play without interrupting the, for example, music of a player. Games like Subway Surfer, Clash Royale and such seem to have this achieved somehow, but I could not achieve it via OpenSL ES, or MediaPlayer.
Upvotes: 0
Views: 1119
Reputation: 11
In order to play sound in background you can use SoundPool, AudioTracks and OpenSlES.
Soundpool: Use small files and make a sequence. In my last project i use 148 sound files (all small) in different scenarios and played using soundpool. Make a list and play one by one or in parallel. Also in games usually you have a small set of sound for particular scenario and it loops. Best is soundpool for that. You can also perform some effects like rate change. Also ogg files are way small, so use them.
AudioTrack: You can use RAW PCM data in audio track. If you want you can extract pcm data using MediaExtractor from almost all formats and use it. But it will be a little work for you in your case, but it is really good (supports huge data, even live streaming).
OpenSLES: Apparently android uses opensles in background for all its purpose. So using it will help you a lot. But it's not easy to get everything done on it. You need to learn more for lesser work.
I have been deeply working on OpenSlES for about 20 days and still i will say for small purpose use soundpool, medium to high level implementation use AudioTracks and for kickass implementation use OpenSLES.
PS: It will be bad effect on your user if you play game sound in background while they are playing their music or their call. Personal experience.
Upvotes: 1