I'm writing code for a game, and I'm trying to load the sounds. I want the background music to start playing immediately - so I wrote this code:
//load the sounds
sPool = new SoundPool(3, AudioManager.STREAM_MUSIC,0);
explosionID = sPool.load(context, R.raw.explosion, 1);
swapID = sPool.load(context, R.raw.swap,1);
sPool.setOnLoadCompleteListener(new OnLoadCompleteListener()
{
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status)
{
//start playing the background music
sPool.play(backgroundMusicID, 0.5f, 0.5f, 3, 1, 1f);
}
});
backgroundMusicID = sPool.load(context, R.raw.alchemists_tower,1);
The other sounds are loading and working just fine, but the background music never seems to start. I put a breakpoint in the listener and it never seems to trigger. The file itself is around half a mb ('explosion' is 250 kb and it works just fine). The backgroundmusic is given a correct id too (checked that with the debugger).
The only time when it appears to be triggering is after the Activity is unloaded (when I load another activity). What am I doing wrong?
Okay, finally solved it. Reason was that the AudioFlinger was failing to load with an error -12.
Ended up having to swap to using a Media Player for the background music, since I have no idea how I ran out of memory.