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?

this will retry 'play' 1000 times on a 10ms interval. this should be run on a non-ui thread of course, and is still not ideal. but maybe a little stronger than waiting an arbitrary time and expecting the pool to be ready.

The SoundPool library uses the MediaPlayer service to decode the audio into a raw 16-bit PCM mono or stereo stream which we simply can call preparation of Stream, that takes some times depending on size of sound file. And if we try to play the sound before that process is over, we get "sample x not ready" error.

There are two solutions for this

implement setOnLoadCompleteListener or

wait arbitrary amount of time so that preparation is over

and then play it
Note that there is no exception for this condition or application is not crashing without any try-catch

This sounds crazy, but don't play sounds right away. Give your app a couple of seconds to initialize the SoundPool. In my app, this was exactly the issue; I added a fake "loading" screen of ~3 seconds, and then everything worked. (I didn't even need to preload the sounds.)

That's what I'm doing to but I find it really annoying. The game loads in a second. The loading screen is only there for the sounds sake.
–
W.K.SApr 16 '12 at 18:40

@W.K.S me too. I had an idea to make the loading screen a mini-game or put a bit of interactivity in it as an "easter egg" kinda thing, even though it's only visible for a couple of seconds.
–
ashes999Apr 16 '12 at 18:52