How to play audio in Java Application

Posted by user577829 on Stack Overflow See other posts from Stack Overflow or by user577829
Published on 2011-01-16T21:41:51Z Indexed on 2011/01/16 21:53 UTC
Read the original article Hit count: 261

Filed under:
|

I'm making a java application and I need to play audio. I'm playing mainly small sound files of my cannon firing (its a cannon shooting game) and the projectiles exploding, though I plan on having looping background music. I have found two different methods to accomplish this, but both don't work how I want.

The first method is literally a method:

        public void playSoundFile(File file) {//http://java.ittoolbox.com/groups/technical-functional/java-l/sound-in-an-application-90681

        try {
//get an AudioInputStream
            AudioInputStream ais = AudioSystem.getAudioInputStream(file);
//get the AudioFormat for the AudioInputStream
            AudioFormat audioformat = ais.getFormat();
            System.out.println("Format: " + audioformat.toString());
            System.out.println("Encoding: " + audioformat.getEncoding());
            System.out.println("SampleRate:" + audioformat.getSampleRate());
            System.out.println("SampleSizeInBits: " + audioformat.getSampleSizeInBits());
            System.out.println("Channels: " + audioformat.getChannels());
            System.out.println("FrameSize: " + audioformat.getFrameSize());
            System.out.println("FrameRate: " + audioformat.getFrameRate());
            System.out.println("BigEndian: " + audioformat.isBigEndian());
//ULAW format to PCM format conversion
            if ((audioformat.getEncoding() == AudioFormat.Encoding.ULAW)
                    || (audioformat.getEncoding() == AudioFormat.Encoding.ALAW)) {
                AudioFormat newformat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
                        audioformat.getSampleRate(),
                        audioformat.getSampleSizeInBits() * 2,
                        audioformat.getChannels(),
                        audioformat.getFrameSize() * 2,
                        audioformat.getFrameRate(), true);
                ais = AudioSystem.getAudioInputStream(newformat, ais);
                audioformat = newformat;
            }

//checking for a supported output line
            DataLine.Info datalineinfo = new DataLine.Info(SourceDataLine.class, audioformat);
            if (!AudioSystem.isLineSupported(datalineinfo)) {
                //System.out.println("Line matching " + datalineinfo + " is not supported.");
            } else {
                //System.out.println("Line matching " + datalineinfo + " is supported.");
//opening the sound output line
                SourceDataLine sourcedataline = (SourceDataLine) AudioSystem.getLine(datalineinfo);
                sourcedataline.open(audioformat);
                sourcedataline.start();
//Copy data from the input stream to the output data line
                int framesizeinbytes = audioformat.getFrameSize();
                int bufferlengthinframes = sourcedataline.getBufferSize() / 8;
                int bufferlengthinbytes = bufferlengthinframes * framesizeinbytes;
                byte[] sounddata = new byte[bufferlengthinbytes];
                int numberofbytesread = 0;
                while ((numberofbytesread = ais.read(sounddata)) != -1) {
                    int numberofbytesremaining = numberofbytesread;
                    sourcedataline.write(sounddata, 0, numberofbytesread);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

The problem with this is that my entire program stops until the sound file is finished, or at least nearly finished.

The second method is this:

    File file = new File("Launch1.wav");
    AudioClip clip;
    try {
        clip = JApplet.newAudioClip(file.toURL());
        clip.play();
    } catch (Exception e) {
        e.getMessage();
    }

The problem I have here is that every time the sound file ends early or doesn't play at all depending on where I place the code.

Is their any way to play sound without the above mentioned problems? Am I doing something wrong? Any help is greatly appreciated.

© Stack Overflow or respective owner

Related posts about java

Related posts about sound