Hello,

I am programming a game in Java. I have made a simple test class to show the problem I am experiencing. Upon hitting a jbutton, i want a song to restart. It will do so, but it is horribly distorted. Below is my code:

 
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.sound.sampled.*;
import java.util.Random;
import javax.imageio.ImageIO;
 
public class SimpleTest extends JFrame  implements ActionListener
{
	AudioFormat audioFormat;
	AudioInputStream audioInputStream;
	SourceDataLine sourceDataLine;
	boolean stopPlayback = false;
	public static long lastMicrosecondPosition=0;
        public static long currentMicrosecondPosition=0;
 
public static void main(String[] args)
{
 
	new SimpleTest();
 
}
 
public SimpleTest()
{
 
	this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(600,600);
        this.setVisible(true);
	JButton Attack=new JButton("Song 1");
        Attack.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent e){
			stop();
			playAudio();
		}});
 
	JPanel container = new JPanel();
	container.setLayout(new GridLayout(1,3));
	container.setSize(400, 500);// 400 width and 500 height
	container.setVisible(true);
	container.add(Attack);
	this.add(container);
}
 
private void playAudio()
{
	try
	{
    	File soundFile =new File("Audio2.wav");
        audioInputStream = AudioSystem.getAudioInputStream(soundFile);
        audioFormat = audioInputStream.getFormat();
        System.out.println(audioFormat);
        DataLine.Info dataLineInfo = new DataLine.Info( SourceDataLine.class,audioFormat);
        sourceDataLine = (SourceDataLine)AudioSystem.getLine(dataLineInfo);
 
 
        new PlayThread().start();
      }
      catch (Exception e)
      {
      	e.printStackTrace();
        System.exit(0);
      }//end catch
  }//end playAudio
 
 
 
  class PlayThread extends Thread
  {
  	byte tempBuffer[] = new byte[5000];
 
    public void run()
    {
    	try
    	{
        	sourceDataLine.open(audioFormat);
        	sourceDataLine.start();
 
        	int cnt;
        	//Keep looping until the input read method
        	// returns -1 for empty stream or the
        	// user clicks the Stop button causing
        // stopPlayback to switch from false to
        	// true.
        	while((cnt = audioInputStream.read(
             	tempBuffer,0,tempBuffer.length)) != -1
                         && stopPlayback == false)
             {
          		if(cnt > 0)
          		{
            //Write data to the internal buffer of
            // the data line where it will be
            // delivered to the speaker.
            sourceDataLine.write(
                               tempBuffer, 0, cnt);
          		}//end if
        	   }//end while
        //Block and wait for internal buffer of the
        // data line to empty.
        sourceDataLine.drain();
        sourceDataLine.close();
 
        //Prepare to playback another file
        //stopBtn.setEnabled(false);
      //  playBtn.setEnabled(true);
       // stopPlayback = false;
      }
      catch (Exception e)
      {
      	e.printStackTrace();
        System.exit(0);
      }//end catch
    }//end run
  }//end inner class PlayThread
//===================================//
 
 /** Stop the audio. */
      protected void stop()
      {
         if (stopPlayback)
         {
            if (sourceDataLine!= null)
            {
               lastMicrosecondPosition=sourceDataLine.getMicrosecondPosition();
              sourceDataLine.stop();
               sourceDataLine.flush();
            }
            stopPlayback = false;
 
 
         }
         }
         public void actionPerformed(ActionEvent e){
		 }
	 }