2

Hello I have kind a weird problem.

import java.io.*;
import javax.sound.sampled.*;

public class Sound
{
    public void newGame()
    {
        AudioInputStream audioInputStream = null;

        try  
        {
            audioInputStream = AudioSystem.getAudioInputStream(getClass().getClassLoader().getResource("files" + File.separator + "newGame.wav"));
            AudioFormat audioFormat = audioInputStream.getFormat();
            DataLine.Info info = new DataLine.Info(Clip.class, audioFormat);
            Clip clip = (Clip) AudioSystem.getLine(info);
            clip.open(audioInputStream);
            clip.start();
        } 
        catch (UnsupportedAudioFileException | IOException | LineUnavailableException e) 
        {
            System.out.println(e.getMessage());
            System.exit(-1);
        } 
        finally 
        {
            try
           {
                if(audioInputStream != null)
                {
                    audioInputStream.close();
                }
            } 
            catch (IOException e) 
            {
                System.out.println(e.getMessage());
                System.exit(-1);
            }
        }
    }
}

I use this code to play sound at the beggining of the new game.

When I run my project from Netbeans IDE everything works just fine but when I build the project and I run it by clicking on the .jar file I begin a new game and the sound is played just for like a half second instead of four seconds. I have no idea why.

guderkar
  • 133
  • 1
  • 10
  • Is `clip.start()` supposed to block until playback is finished? – janos Sep 06 '14 at 17:16
  • possible duplicate of [how can I wait for a java sound clip to finish playing back?](http://stackoverflow.com/questions/557903/how-can-i-wait-for-a-java-sound-clip-to-finish-playing-back) – greg-449 Sep 07 '14 at 07:49
  • Well I could wait for the clip to finish but I want it to be played in background and it actually is played in background but in netbeans the clip is played for 4 seconds (full length) and outside a netbeans it is played just for a half second. Only solution that comes to my mind is a start a new thread each time i want to play the clip but I think it is a bad solution. – guderkar Sep 07 '14 at 18:02

0 Answers0