I have a .wav file that I'd like to use across my game, currently I am loading the sound in onCreate()
of each activity in the game.
soundCount = soundpool.load(this,R.raw.count, 1);
The sound will be played once the activity starts.
soundpool.play(soundCount, 0.9f, 0.9f, 1, -1, 1f);
Problem is at times I will hit the error "sample x not ready"
. Is it possible to load the .wav file once upon starting the game and keep it in memory and use it later across the game? Or is it possible to wait for 1-2 seconds for the sound to load finish?
You'll need to wait for it to finish by adding a listener via SoundPool.setOnLoadCompleteListener
.
Since my project is compatible with Android 1.5 and I couldn't use setOnLoadCompleteListener, I resolved making the play of sound delayed. My source code follows:
playSound_Delayed(soundId, 100);
// (..)
private void playSound_Delayed (final int soundId, final long millisec) { // TIMER final Handler mHandler = new Handler(); final Runnable mDelayedTimeTask = new Runnable() { int counter = 0; public void run() { counter++; if (counter == 1) { boolean ret = mHandler.postDelayed(this, millisec); if (ret==false) Log.w("playSound_Delayed", "mHandler.postAtTime FAILED!"); } else { playSound(soundId); } } }; mDelayedTimeTask.run(); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With