Issue With Sound Manager Playing Random Files
I am trying to use the SoundManager to randomly play a 30 second sound bite while simultaneously starting an animation sequence (flashing graphic) triggered by an onTouch event. F
Solution 1:
There's a good chance that the audio hasn't been loaded. If you really want to play a number of short clips, definitely look at SoundPool instead. (Adapted from Google SoundPool Example)
Try this:
publicclassSoundboardextendsActivity {
private SoundPool mSoundPool;
booleanloaded=false;
privateint[] soundIDs;
privateint[] resIDs;
/** Called when the activity is first created. */@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSoundPool = newSoundPool(10, AudioManager.STREAM_MUSIC, 0);
mSoundPool.setOnLoadCompleteListener(newOnLoadCompleteListener() {
@OverridepublicvoidonLoadComplete(SoundPool soundPool, int sampleId,
int status) {
loaded = true;
}
});
resIDs = newint[]{R.raw.sound0,R.raw.sound1,R.raw.sound2.R.raw.sound3 };
for(int i=0; i<resIDs.length; i++){
soundIDs[i] = mSoundPool.load(this, resIDs[i], 1);
}
};
publicbooleanonTouchEvent(MotionEvent evt){
Randomr=newRandom();
intx= r.nextInt(resIDs.length);
switch (evt.getAction())
{
case MotionEvent.ACTION_DOWN:
AudioManageraudioManager= (AudioManager) getSystemService(AUDIO_SERVICE);
floatactualVolume= (float) audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC);
floatmaxVolume= (float) audioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
floatvolume= actualVolume / maxVolume;
// Is the sound loaded already?if (loaded) {
mSoundPool.play(soundIDs[x], volume, volume, 1, 0, 1f);
Log.i("Test", "Played sound");
}
startAnimating();
returnsuper.onTouchEvent(evt);
case MotionEvent.ACTION_UP:
break;
default:
break;
}
returntrue;
}
privatevoidstartAnimating() {
ImageViewwiub_screen01= (ImageView) findViewById(R.id.wiub_screen01);
Animationfadein01= AnimationUtils.loadAnimation(this, R.anim.fade_in01);
wiub_screen01.startAnimation(fadein01);
ImageViewwiub_screen00= (ImageView) findViewById(R.id.wiub_screen00);
Animationfadein00= AnimationUtils.loadAnimation(this, R.anim.fade_in00);
wiub_screen00.startAnimation(fadein00);
}
}
Post a Comment for "Issue With Sound Manager Playing Random Files"