Полезная информация

Exploring Java

Previous: 17.6 Filtering Image DataChapter 17
Working with Images
Next: 18. Java Beans

17.7 Working with Audio

So you've read all the material on drawing and image processing, and you're wondering what in the world audio has to do with images. Well, not much actually, except that true multimedia presentations often combine image techniques such as animation with sound. So we're going to spend a few minutes here talking about audio, for lack of a better place to discuss it.

As we write this, the good people at Sun are hard at work developing the API that Java applets will use for playing audio. A future release of Java will have support for real-time and continuous audio streams, sound management, mixing, synchronization, and filtering. Unfortunately, at the moment, we can tell you only about the basics.

java.applet.AudioClip defines an interface for objects that can play sound. An object that implements AudioClip can be told to play() its sound data, stop() playing the sound, or loop() continually.

An applet can call its getAudioClip() method to retrieve sounds over the network. This method takes an absolute or relative URL to specify where the audio file is located. The viewer may take the sound from a cache or retrieve it over the network. The following applet, NoisyButton, gives a simple example:

import java.awt.*;
import java.awt.event.*;
public class NoisyButton extends java.applet.Applet implements ActionListener { 
    java.applet.AudioClip sound;
    public void init() {
        sound = getAudioClip( getClass().getResource(getParameter("sound")) );
        Button button = new Button("Play Sound");
        button.addActionListener( this );
        add ( button );
    public void actionPerformed( ActionEvent e ) {
        if ( sound != null )

NoisyButton retrieves an AudioClip from the server; we use getParameter() to supply the name of the sound and getResource() to locate it. (The applet tag that displays NoisyButton must include a parameter tag for sound.) When the button is pushed, we simply call the play() method of the AudioClip to start things. After that it will play to completion unless we call the stop() method to interrupt it. Unfortunately, this is about the extent of what we can do with sound right now. If you want to experiment, there are a few additional methods in the sun.audio classes. Stay tuned for a bigger and better audio API as part of the upcoming Java Media package.

Previous: 17.6 Filtering Image DataExploring JavaNext: 18. Java Beans
17.6 Filtering Image DataBook Index18. Java Beans

Other Books in this LibraryJava in a NutshellJava Language ReferenceJava AWTJava Fundamental ClassesExploring Java