Java
Java intro
Java basics
Java variables
Java conditionals
Java loops
Java arrays
Java OOP 1
Java OOP 2
Java interfaces
Java Strings
Java user input
Java exceptions
Java packages
Java rand numbers
Java GUI's
Java GUI layout
Java events
Java applets
Java graphics
Java sounds
Java summary

Programming

Programming intro

Markup

First webpage guide HTML
XHTML

Style & Layout

CSS

Browser scripting

Javascript
VBScript
AJAX

Server scripting

PHP
ASP

Making money online

Make money online

Java sounds

Java provides the ability to play sounds in applications and applets.

This lesson focuses on:

The AudioClip interface

To be able to play a sound in a program or in applet, you first have to use the AudioClip interface and instantiate an AudioClip object with it. The AudioClip interface is located in the java.applet package.

Example:

AudioClip aClip;

The newAudioClip method

After instantiating an AudioClip object, you have to upload the actual sound to be played. This is achieved with the newAudioClip method - a static method of the Applet class.

Example:

aClip = Applet.newAudioClip(url("file:sound.wav"));

In the above example, a file named sound.wav is uploaded to be stored in the aClip object.

Methods of the AudioClip interface

Once a sound is uploaded, you can use the methods of the AudioClip interface to work with it.

Methods of the AudioClip interface:

Example:

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.net.*;

/*

The java.net package is imported 
into this program so that the URL
class can be used

*/

class PlayAndStopSoundFrame extends Frame 
implements ActionListener{

    AudioClip powerChords;

    //create three buttons
    //to control if the sound
    //is playing or not
    Button play, loop, stop;

    public PlayAndStopSoundFrame(){

        play = new Button("Play");
        play.addActionListener(this);
        add(play);

        loop = new Button("Loop");
        loop.addActionListener(this);
        add(loop);

        stop = new Button("Stop");
        stop.addActionListener(this);
        add(stop);

    try{
        
        //try to upload a file 
        //named "powerchords.wav" to be played
        powerChords = Applet.newAudioClip(new URL
        ("file:powerchords.wav"));

    }

    //if there is a problem with the URL
    //then this is the exception to be used
    catch (MalformedURLException mfe){}

    setLayout(new FlowLayout());
    setSize(220, 150);
    setVisible(true);

    }

    public static void main(String[] args){

    //create a new instance of PlayAndStopSoundFrame
    PlayAndStopSoundFrame PASSF = 
    new PlayAndStopSoundFrame();

    }

    public void actionPerformed(ActionEvent e){
  
        /*

        check which button is pressed
        and play or stop the sound accordingly

        */

        if (e.getSource() == play){
        
            powerChords.play();

        }

        if (e.getSource() == loop){

            powerChords.loop();

        }

        if (e.getSource() == stop){

            powerChords.stop();

        }

    }

}

Output:

Sound frame

In the above example, a new package that has not been used in any of the previous lessons is imported - the java.net package. This package contains several classes and interfaces for networking. It is imported in this case to use the URL class to upload the sound into the program.

A class named PlayAndStopSoundFrame which extends the Frame class and implements the ActionListener interface is created. An AudioClip object named powerchords is created, as well as three button objects named play, loop, and stop. The constructor for the class instantiates the buttons, adds an action listener to them, and adds them to a frame. The constructor then tries to upload a sound named powerchords.wav through the newAudioClip method together with the URL class and catches a possible exception with the MalformedURLException exception (if there is an error with the URL) The constructor also sets the layout of the frame to flowlayout, sets the size of it to 220 by 150, and sets it to be initially visible.

The main() method creates a new instance of the PlayAndStopSoundFrame class.

The actionPerformed() method is used to check which button was pressed and to play or stop a sound accordingly.

Practice

Online code editor
Practical examples
Practical exercises
Step-by-step tutorials

Reference

Terms glossary
Reference material

Rate this site

Rate this site
Visitor comments