Hi,
I’m just trying to create my first game but I still have some problems with sound. I wrote my own class for playing a wav-file:
import java.io.IOException;
import java.net.*;
import javax.sound.sampled.*;
public class Jukebox extends Thread{
Clip clip;
AudioFormat format;
AudioInputStream ais;
boolean play = false;
//init Class
public Jukebox (URL url){
ais = null;
try {
ais = AudioSystem.getAudioInputStream(url);
} catch (Exception e){}
format = ais.getFormat();
DataLine.Info info = new DataLine.Info(Clip.class,format,(int)(ais.getFrameLength()*format.getFrameSize()));
try {
clip = (Clip)AudioSystem.getLine(info);
} catch (LineUnavailableException e1) {}
try {
clip.open(ais);
} catch (LineUnavailableException e2) {}
catch (IOException e2) {}
}
public void run() {
//Loop until game is terminated
while(true){
//play is true?
if(play){
//got to start
clip.setFramePosition(0);
clip.start();
//loop as long Clip is running
while(true){
if(clip.isRunning()){
try {
Thread.sleep(10);
} catch (InterruptedException e1) {}
}else{
break;
}
}
clip.stop();
}//play
try {
Thread.sleep(10);
} catch (InterruptedException e) {}
}//while
}
public void startClip(){
play = true;
}
public void stopClip(){
play = false;
}
}
This class works fine the first time. But if the sound should be played again from the speakers only comes some kind of interferences (or athmospherics). What’s wron with my class?
Ralf