Playing AudioClips
When I play AudioClips in my new game I have a hard time finding clips that work properly. So far i only have about three clips that play consistently and when i want them to play. Most clips only play about once out of ten of the times that i want them to play. I have tried different types of audioformats and different settings for them, all in vain. What to do?
here is a code snippet that shows what my code looks like:
URL base =null;
try
{
base =new URL("file:" + System.getProperty("user.dir") +"/sfx/gunshot.wav");
}catch(java.net.MalformedURLException mue)
{
System.out.println("INVALID URL");
}
boom = Applet.newAudioClip(base);
And then i play the sound using boom.play(). this only work in about 10% of the times except for certain clips that always work.
What have i done wrong? I磎 using Swing but still use the Applet.new AudioClip(URL), it shouldn磘 be a problem, i made an Applet game before and used a different method there, still with the same result.
please help me...
[1414 byte] By [
fet_loaa] at [2007-10-2 0:04:23]

i don't rreally know but the AudiClip class in Applet is pretty old... i recommend you use javax.sound.sampled it's much better
i'm here's my SoundClip class:
//
// SoundClip.java
// AsondSoftPackage
//
// Created by Marianne Gagnon on Fri Oct 08 2004.
// Copyright (c) 2004 AsondSoft. All rights reserved.
//
import javax.sound.sampled.*;
import java.io.*;
public class sSoundClip {
AudioInputStream audio_snd;
Clip audio_clip;
//
public sSoundClip(String path){
try {
audio_snd = AudioSystem.getAudioInputStream(new File(path));
AudioFormat audio_format=audio_snd.getFormat();
DataLine.Info info = new DataLine.Info( Clip.class, audio_snd.getFormat(), ((int)audio_snd.getFrameLength()*audio_format.getFrameSize()));
audio_clip = (Clip) AudioSystem.getLine(info);
audio_clip.open(audio_snd);
} catch (Exception e) {sIO.alert("ALERT:\n\n Could not load sound "+new File(address).getName()+" .\n\nError message: \n "+e);}
}
//
public void play(){
audio_clip.setFramePosition(0);
audio_clip.start();
}
//
public void playFromPosition(){
audio_clip.start();
}
public void playFromBeginning(){
audio_clip.setFramePosition(0);
audio_clip.start();
}
public void playLoopForever(){
audio_clip.loop(Clip.LOOP_CONTINUOUSLY);
}
public void playLoop(int i){
audio_clip.loop(i-1);
}
//
public void setPosition(int pos){
audio_clip.setFramePosition(pos);
}
//
public void setPositionInPercent(float pos){
audio_clip.setFramePosition((int)(pos/100*audio_clip.getFrameLength()));
}
//
public void stop(){
audio_clip.stop();
}
//
public boolean isPlaying(){
//return(audio_clip.getFramePosition()<audio_clip.getFrameLength()-2000);
return audio_clip.isActive();
}
//
public void setVolume(int vol){
FloatControl volume = (FloatControl)audio_clip.getControl(FloatControl.Type.MASTER_GAIN);
volume.setValue(vol*(volume.getMaximum()-volume.getMinimum())/100+volume.getMinimum());
}
//
public void setPan(int pan){
FloatControl volume = (FloatControl)audio_clip.getControl(FloatControl.Type.PAN);
volume.setValue(pan*(volume.getMaximum()-volume.getMinimum())/100+volume.getMinimum());
}
}>