Good morning,
Im having a problem playing sounds, there is somewhere a memmory leak, but i cant seem to find out where it is. I have closed everything and catched everything off. Can anybody see my problem?
This is how i play a sound:
Sound sound = new Sound("NotenoughMoney.wav",5000);
and this is my sound class:
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineListener;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
public class Sound extends Thread{
boolean playing;
int length, time;
Clip clip = null;
AudioInputStream sound = null;
File soundFile = null;
DataLine.Info info = null;
Sound(String location, int length){
soundFile = new File(location);
this.length = length;
playing = true;
this.start();
}
private void play(){
try {
sound = AudioSystem.getAudioInputStream(soundFile);
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
info = new DataLine.Info(Clip.class, sound.getFormat());
try {
clip = (Clip) AudioSystem.getLine(info);
} catch (LineUnavailableException e) {
e.printStackTrace();
}
try {
clip.open(sound);
} catch (LineUnavailableException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
clip.addLineListener(new LineListener(){public void update(LineEvent event){}});
clip.start();
}
public void run(){
play();
while(playing){
time++;
try {Thread.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}
if(time>=length)
playing = false;
}
System.out.println("CLOSE");
close();
}
private void close(){
clip.stop();
soundFile.delete();
clip.close();
try {
sound.close();
} catch (IOException e) {e.printStackTrace();}
}
}
Does anybody see my problem here? ???