How to play multiple sounds simultaneously in Android? - android

I've been trying to make multiple sounds work simultaneously but I kept getting errors like Sample not ready and Error creating AudioTrack. Though I've taken care of the first error, the other error persists.
I'm using two mp3 files, that are away above 5 mb(>4 minutes) and thus was wondering if SoundPool is the right way to go about it? I can use Media Manager but it doesn't let me play multiple files simultaneously.

SoundPool is recommended for <1MB sound clips, so I'd use MediaPlayer in your case. You can call the following for each sound and they will play simultaneously:
MediaPlayer.create(YourActivity.this, R.raw.your_sound).start();
Or you can create multiple MediaPlayer objects and play a sound on each, then release() them.

Related

Android MediaPlayer synchronized start

I'm trying to use Android's MediaPlayer to play a backing audio track to a music game which should stay in sync with other events.
However, I'm finding that on different devices the backing track seems out of sync.
After some frustration, I'm coming to the conclusion that the time it takes to start playing the audio is indeterminate.
Is there a way around this? Or some kind of callback from the MediaPlayer that represents "starting to play NOW"?
AFAICT I can't use the SoundPool for this backing track as the audio file is too long.

Android : How to play multiple mp3 tracks simultaneously

In the application, songs will have been split into multiple tracks, one track per instrument.
I need to be able to play all the tracks simultaneously.
I have seen that MediaPlayer is not able to do this, indeed having 5 to 10 separate tracks would cause too much latency.
SoundPool states that it is specifically for short sounds and so is inappropriate for this.
It would appear that AudioTrack would be the answer, but that requires decoding each mp3 into pcm data before playing it.
So, here are my questions -
Even though I have not found any, do any tutorials exist demonstrating how to achieve this ?
Can anyone advise on how to play multiple audiotracks simultaneously ?
Are there any existing examples on how to decode mp3 files using the NDK ?

Play audio in background without interrupting mediaplayer

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.
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.

Soundpool issue

I'm working on a little game that is uses soundpool to play small sound-files.
However, it now seems that I have too many sound files that I want to add to the soundpool since I get an error saying that the heap has an overflow.
So then I tried to load the sound files when they are needed instead of loading them when the class instance is initialized, but the result of that turned out very bad.
Are there any other ways I can make it work using soundpool or do I have to use media player instead? I already have media player class that is used to play long sound clips like music files and so on. So the backup plan is to make two instances of the media player class that runs on two separate threads where one of them handles the small sound files.
Any help and ideas is highly appreciated.
Greetings!
Try dividing the SoundPool object you are using into multiple instances. Ex. SoundPool sp1, sp2, sp3; Do not use multiple MediaPlayer objects as this will be very slow and inefficient.

Android, load sounds and play immediately?

I have been attempting to play a bunch of sounds in my app as some views switch. The sound is narrative for the views and is between 3 and 10 seconds each, of which there are about 30.
First, I tried loading them all into a sound pool and passing it to a hashmap on program load, then I simply play them when required. This worked for the first 5 or 6 sounds but once I started to add more sounds to the sound pool, the later ones did not play. I assume this is due to the 1MB limit I have read about on soundpool.
I tried switching to just loading the sound and passing it straight to play on the next line however, no sounds play. Logcat just shows a reset and command cancelled for the player.
I switched then to loading the file and pointing to it with the hash map, however, even after doing an unload and loadimg a new sound at the same index it would just play the same sound every time.
I have tried MediaPlayer but it is ineffective for my desired application.
Can anyone suggest a way I should look to implement this properly? And should I be trying to load all sounds before hand or not?
I think you need to wait for the load to complete before you can play it. Add an onLoadCompleteListener and then play it when that is invoked.

Categories

Resources