vendredi 19 décembre 2014

SoundPool Sample loads in onCreate but then says "Sample not ready" When Called

I'm trying to make a sound effect play using sound pool when a button is clicked (it's eventually meant to play on a repeating timer task, but I'm trying to get it working here first) but it keeps logging the message "sample 1 not ready" to LogCat even though it should only be trying to play if it was loaded in onCreate and set my loaded boolean to true, and the message also my Log.e("Test", "Played sound") is also displaying even though it shouldn't unless the sound is loaded.


Have I misunderstood, are you meant to be loading the sound repeatedly or specifically storing the sound in some other form that lasts longer or is there just some slip up I have here that's made it go funny?



protected void onCreate(Bundle savedInstanceState) {
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
loaded=true;
}
});
soundID = soundPool.load(this, R.raw.blop, 1);
}

public void onClick(View v) {
// Getting the user sound settings
AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
float actualVolume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
float maxVolume = (float) audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
volume = actualVolume / maxVolume;
// Is the sound loaded already?
if (loaded){
soundPool.play(soundID, volume, volume, 1, 0, 1f);
Log.e("Test", "Played sound");
}
}

Aucun commentaire:

Enregistrer un commentaire