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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
| 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;
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() {
while(true){
if(play){ clip.setFramePosition(0); clip.start(); while(true){ if(clip.isRunning()){ try { Thread.sleep(10); } catch (InterruptedException e1) {} }else{ break; } } clip.stop(); } try { Thread.sleep(10); } catch (InterruptedException e) {} } } 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