Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Soundpool sample not ready

Tags:

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?

like image 988
SteD Avatar asked Mar 05 '11 08:03

SteD


2 Answers

You'll need to wait for it to finish by adding a listener via SoundPool.setOnLoadCompleteListener.

like image 76
EboMike Avatar answered Nov 09 '22 01:11

EboMike


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(); } 
like image 27
Lisitso Avatar answered Nov 09 '22 02:11

Lisitso