Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Playing simple (sampled) Audio in Java


  • Please log in to reply
4 replies to this topic

#1 farrell2k

farrell2k

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 169 posts

Posted 12 August 2010 - 12:04 PM

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.
  • 0

#2 Khaotic

Khaotic

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 62 posts

Posted 22 August 2010 - 10:22 AM

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

#3 Jarryd

Jarryd

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 63 posts
  • Location:Australia
  • Programming Language:C, Java, C++, C#

Posted 17 October 2010 - 01:10 PM

Nice work mate, very well explained..!
  • 0

#4 Edgod

Edgod

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 19 August 2014 - 03:38 AM

Sorry for Necro, but how would you play the clip in a different class?   I have loaded the clip and it plays on start up as shown, however if I want it when I try to play it from a different class it fails to find the clip.

 

I try to access it using Main.clip.start(); and it says clip cannot be resolved.  Any help would be appreciated.  


  • 0

#5 WendellHarper

WendellHarper

    CC Newcomer

  • Member
  • PipPip
  • 16 posts
  • Location:Leicester
  • Programming Language:Visual Basic .NET
  • Learning:Python

Posted 10 September 2014 - 02:22 AM

Thank you for sharing this useful post. This helped me a lot. Cheers


  • 0

Wendell Harper - Team Member at www.printingperiod.co.uk