I am writing a game for 7650 and and using the Sound API to produce some sounds on certain events like when character punches the other
*******************
First i load the sound as
*******************
tune = this.getClass().getResourceAsStream("/Dragon1/res/scn/ss2.wav");
buffer4 = new byte[8000];
tune.read(buffer4, 0, buffer4.length);
sound4 = new Sound(buffer4, Sound.FORMAT_WAV);
sound4.init(buffer4, Sound.FORMAT_WAV);
sound4.setSoundListener(this);
*******************
then i play the sound as
*******************
public void playSound(int son)
{
if(blnEnableSounds == false)
return;
if(blnSoundPlaying == true)
return;
if(son == 1)
sound1.play(1);
//else if(son == 2)
// sound2.play(1);
else if(son == 3)
sound3.play(1);
else if(son == 4)
sound4.play(1);
blnSoundPlaying = true;
}
*******************
so i play the sound by calling the function with this the no of sound to play..
*******************
*******************
also notice i used a variable that indicates whether the sound is still playing or not, blnSoundPlaying is used to do so.... i make it true
Also the class implements SoundListener and in event
*******************
public void soundStateChanged(Sound sound, int event)
{
if(event == Sound.SOUND_STOPPED)
blnSoundPlaying = false;
}
*******************
so to make sure that not two sounds play at the same time
BUT sounds are broken i mean play half or play late when it supposed to play.. it does not happen all time ... it just happen randomly
*******************
Shahzad

Reply With Quote

