Jump to content

Playing simple (sampled) Audio in Java

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
farrell2k

farrell2k

    Learning Programmer

  • Members
  • PipPipPip
  • 60 posts
I used to think that playing audio was difficult, until I took the time to actually read the JavaDocs and played around a bit in netbeans. You can play sampled audio in your swing applications by using a few classes from the javax.audio.sampled package.

From the java tutorial Playing Back Audio (The Java™ Tutorials > Sound), if you just need to play an audio file and you are not concerned with doing any advanced mixing, you can use a Clip, an object that represents and buffered audio file in memory. O.K., so how to we create a clip? According to the javadocs, there are two ways to get a clip: from an AudioSystem object, or from a Mixer. Since I am only concerned with playing the audio, and I am not interested in mixing or manipulating it in any way, I chose to use AudioSystem. I looked at the example in the java sound tutorial and instantly became confused. :) I decided to examine the AudioSystem class instead, and lo and behold, AudioSystem has a static method getClip() that returns a clip. I don't have to worry about specifying what mixer to use as it uses the system default, and I don't have to worry about TargetDataLines, etc. Nice. so I create my clip with:

Clip clip = AudioSystem.getClip();
Now that I have my clip, I examine the Clip Interface and see that there are methods for playing, looping, stopping audio files. Fantastic, but how to I get clip to play my file? It turns out that Clip has a method called open() that opens an AudioInutStream that contains the data of the file I want to play. OK so I check out AudioInputStream's constructors and decide that there must be a way to create an AudioInputStream by just specifying a file. I check the AudioSystem class and there certainly is: AudioSystem.getAudioInputStream(File file). Fantastic. I set up my AudioInputStream like so:

AudioInputStream audio = AudioSystem.getAudioInputStream(new File("x.wav"));
Now I can take my clip object and open the AudioInputStream, then I can play() it.

Here's my code.

public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setSize(300,300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

        KeyListener s;

        try {
            AudioInputStream audio = AudioSystem.getAudioInputStream(new File("x.wav"));
            Clip clip = AudioSystem.getClip();
            clip.open(audio);
            clip.start();
        }
        
        catch(UnsupportedAudioFileException uae) {
            System.out.println(uae);
        }
        catch(IOException ioe) {
            System.out.println(ioe);
        }
        catch(LineUnavailableException lua) {
            System.out.println(lua);
        }
}
Check out the clip Interface for more methods.

#2
Khaotic

Khaotic

    Learning Programmer

  • Members
  • PipPipPip
  • 62 posts
Thank mate. Helped me understand this a lot more. +1 rep
Check out my site: www.khaoticirc.net

#3
Jarryd

Jarryd

    Learning Programmer

  • Members
  • PipPipPip
  • 63 posts
Nice work mate, very well explained..!