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]
# 1

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());

}

}>

must41a at 2007-7-15 16:03:51 > top of Java-index,Other Topics,Java Game Development...
# 2
forget the copyright ok? it's automatically generated by my IDE and i forgot to remove it however i don't mean it at all ;)
must41a at 2007-7-15 16:03:51 > top of Java-index,Other Topics,Java Game Development...
# 3
Thx for your answer. It didn磘 solve the problem though, still the sounds only play about 10% of the time. I磎 confident that your solution should work very well, so i磎 really confused now. I will have to do some more research =)
fet_loaa at 2007-7-15 16:03:51 > top of Java-index,Other Topics,Java Game Development...
# 4

If you're not using the latest version of Java try upgrading if it is an option. There was a bug in the sound clip code for JDK1.5.0 and 1.6.0 initial versions. I don't recall which update corrected it. It should be correct in the latest though. (That's assuming your clip is < 1 second long).

patrickmallettea at 2007-7-15 16:03:51 > top of Java-index,Other Topics,Java Game Development...
# 5
Ok, thx! I'll have to try that. Unfortunately, my computer died on me yesterday, so i have to find a way to fix it before i can be able to test an upgrade...
fet_loaa at 2007-7-15 16:03:51 > top of Java-index,Other Topics,Java Game Development...