| 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:  
             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:  
             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 |