Game behaves weird when sound is playing

Here is my Sound class (im completely clueless of what all this stuff means).


class Sound{
	private AudioFileFormat baseFileFormat;
	private AudioFormat baseFormat,decodedFormat;
	private AudioInputStream din,in;
	private AudioFileFormat.Type type;
	private float frequency;
	public Sound(String filename){
		try{
			baseFileFormat = AudioSystem.getAudioFileFormat(new File(filename));
		}	catch(UnsupportedAudioFileException ex){
			System.out.println("Audio File not Supported");   
		}	catch(IOException ex2){}
		baseFormat = baseFileFormat.getFormat();
		type = baseFileFormat.getType();
		frequency = baseFormat.getSampleRate();
		in=null;
		try{
			in= AudioSystem.getAudioInputStream(new File(filename));
		}	catch(UnsupportedAudioFileException ex){}
			catch(IOException ex2){}
		din = null;
		baseFormat = in.getFormat();
		decodedFormat =new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
                    baseFormat.getSampleRate(),
                    16,
                    baseFormat.getChannels(),
                    baseFormat.getChannels() * 2,
                    baseFormat.getSampleRate(),
                    false);
		din = AudioSystem.getAudioInputStream(decodedFormat, in);
		//in.close();
	}
	public void play(){
		try{
			rawplay(decodedFormat, din);
		}	catch(LineUnavailableException ex){}
			catch(IOException ex2){}
	}	
	public void stop(){
		try{
			in.close();
		}	catch(IOException ex2){}
	}
	private void rawplay(AudioFormat targetFormat,AudioInputStream din)
   		throws IOException, LineUnavailableException{
 		byte[] data = new byte[4096];
  		SourceDataLine line = getLine(targetFormat);
  		if (line != null){
    		line.start();
    		int nBytesRead = 0, nBytesWritten = 0;
    		while (nBytesRead != -1){
        		nBytesRead = din.read(data, 0, data.length);
        		if (nBytesRead != -1)
            	nBytesWritten = line.write(data, 0, nBytesRead);
    		}
    		line.flush();
    		line.stop();
    		line.close();
    		din.close();
  		}
	}
	private SourceDataLine getLine(AudioFormat audioFormat) throws LineUnavailableException{
  		SourceDataLine res = null;
  		DataLine.Info info =
    		new DataLine.Info(SourceDataLine.class, audioFormat);
  		res = (SourceDataLine) AudioSystem.getLine(info);
  		res.open(audioFormat);
  		return res;
	}
}

When i try playing a sound with it, the game freezes untill the sound stops playing. When i try playing it again, it wont play. The stop() function does not stop the sound. I have another question: how would i implement loop() function (one that works properly). To make long story short: is there a library-thingy for a java application which provides play(), stop() and loop() functions? :frowning:

you may try to launch the player with a new Thread and stop should work.
Try like this :

/** within the Sound class block add a Thread member */
Thread player = null; 
/** add a new Thread for the play() to launch in the background **/
public Thread playInBg() {
// you may add some checks and conditions for avoiding simultaneous multiple threadings
if(player instanceof Thread) {
          if(player.isAlive() && !player.isInterrupted()) {
               player.interrupt(); 
          }
}
Thread t = player = new Thread(new Runnable() {public void run() { play(); Thread.currentThread().interrupt();}}, "T-soundplayer");
t.start();
return t;
}