Home : Course Map : Chapter 12 : Java :
Audio
JavaTech
Course Map
Chapter 12

Printing
  Demo 1
Cursor Icons
  Demo 2
MouseButtons
  Demo 3
PopupMenu
  Demo 4
Keystrokes
  Demo 5
Audio
  Demo 6
Timing & Speed
  Demo 7
Toolkit
  Demo 8
AppletContext
  Demo 9
Exercises

    Supplements
Java Beans
More APIs
Java & Browsers
  Demo 1
     About JavaTech
     Codes List
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New

In the early versions of Java the audio capabilities in the core language were extremely limited. Only 8 kHz au type files could be played.

With version 1.2, it became possible to play 22KHz, 16-bit stereo in the following formats:

  • AIFF
  • AU
  • WAV
  • MIDI
  • RMF

The new sound engine is a part of the core library. A full featured Java Sound API became available in Java 1.3 that include the packages javax.sound.midi and javax.sound.sampled.

However, these advanced audio capabilities are beyond the scope here. We will only look at the simply playing of sound clips.

While audio may have limited applications for scientific programs, they can be useful for such things as warnings and alarms.

The AudioClip interface has 3 methods to be implemented:

  • Play()
  • Loop()
  • Stop()

The applet method

    Applet.getAudioClip(URL url)

returns an instance of an AudioClip object.

The following applet shows the basics of playing a sound clip. The loop() method will continously repeat the clip.

AudioTestApplet.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

/** Demonstrate playing an audio clip. **/
public class AudioTestApplet extends JApplet
{
  public void init () {
    Container content_pane = getContentPane ();
    // Create an instance of a JPanel sub-class
    AudioPanel audio_panel =
       new AudioPanel (getClip (false));
    // And add one or more panels to the JApplet panel.
    content_pane.add (audio_panel);
  } // init

  AudioClip getClip (boolean FileInJar) {
    if (FileInJar) {
        // Use getResource () to search directory or a jar file.
        return (getAudioClip (
                getClass ().getResource (getParameter ("AudioClip")))
             );

    } else {
        // Read  audio file from the code directory
        return (getAudioClip (getCodeBase (),
                              getParameter ("AudioClip")) );
    }
  } // getClip

} // class AudioTestApplet

/** This panel holds a button to play/stop a clip in loop mode.**/
class AudioPanel extends JPanel
                   implements ActionListener
{
  AudioClip fAudioClip = null;
  JButton fButton = null;
  boolean fPlay = false;

  /** Constructor gets the clip and makes a button for the panel.**/
  AudioPanel (AudioClip audioClip) {
    fAudioClip = audioClip;
    // Add a button for the interface.
    fButton =  new JButton ("Play Clip" );
    fButton.addActionListener (this);
    add (fButton);
  } // ctor

  /** Button will start/stop the clip playing.**/
  public void actionPerformed (ActionEvent e ) {
    if (fAudioClip  != null) {
        if (!fPlay) {
            fButton.setText("Stop clip");
            fAudioClip.loop ();
            fPlay = true;
        } else {
            fButton.setText("Play clip");
            fAudioClip.stop();
            fPlay = false;
        }
    }
  } // actionPerformed

} // class AudioPanel

 

See the Class section for a discussion of the getResources() method for accessing files in a JAR file.

References & Web Resources

Latest update: Dec.6.2004

              Tech
Tech APIs
Exercises

           Physics
Math/Science APIs
JAIDA Program
  Demo 1
Demo 1 Discussion
Exercises

  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
21
22 23 24
Supplements

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.