I have tried everithing to make this work. The Android's SoundPool class is loading the .ogg audio file (tested), but it doesn't play. For some strange reason i can't manage to get this working. I have followed many tutorials and forums on the matter, and still can't get it working.
Here's my code:
As Global variables:
SoundPool soundPool;
boolean ya = false;
On my Activity's onCreate:
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
soundPool = new SoundPool.Builder()
.setMaxStreams(15)
.build();
}else{
soundPool = new SoundPool(15, AudioManager.STREAM_MUSIC, 0);
}
soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool sp, int sampleId, int status) {
ya = true;
}
});
soundPool.load(this, R.raw.explosion_1, 1);
Finally, on the onClick method of one of my buttons:
public void instructions(View v){
if(ya){
soundPool.play(R.raw.explosion_1, 0.99f, 0.99f, 1, 0, 0.99f);
}
}
Please help.
In order to play the sound, you need to pass in the id
that you got from load()
into play()
.
int explosion = soundPool.load(this, R.raw.explosion_1, 1);
soundPool.play(explosion, 0.99f, 0.99f, 1, 0, 0.99f);