AudioClip

The AudioClip class is in charge of playing sounds in Java. To play a sound, you have to first find the sound that you want, usually somewhere on the internet, download it, and put it in the same directory as your java files.

Usually sound effect files will have an extension .au or .wav, and music will have a .mp3 or .midi format. Which formats your computer can play depends on the computer; almost all computers can play .midi, .au, and .wav, but on many computers Java isn't able to play .mp3 files (even if you have an mp3-playing program elsewhere on the computer).

The AudioClip class is part of the java.applet package, so you need to import at the top of each java file where you want to use it:

import objectdraw.*;
import java.applet.AudioClip;

public class ....

Table of Contents

1. Loading an AudioClip

To load an AudioClip, first remember that the sound file needs to be in the same directory as whatever java file you are working with. You will load your AudioClip in whatever class you have that extends Controller or WindowController, because they have a method that makes it easy to load a sound:

public AudioClip getSound(String fileName)

So, for example, here is a sound that I downloaded for free from somewhere. If you click it your browser will probably play it for you:

laser_trill.au

I can load this into an AudioClip by just doing this:

AudioClip sound = getAudio("laser_trill.au")

If you have a lot of sounds, you may consider storing them in another directory inside your java project directory; you could call that directory "sounds". You would then have to include that directory name in the path to your sound:

AudioClip sound = getAudio("sound/laser_trill.au")

2. Playing sounds

Your browser doesn't do Java.
SoundMaker.java
AudioClip only has three methods:

public void play()
public void loop()
public void stop()

If you call play(), the sound will play once, then automatically stop. You can also stop it before it is done by calling stop(). On the other hand, if you call loop() the sound will start playing continuously, looping back to the start of the sound once it gets back to the end. Again, you can stop it using the stop() method.

Any sound file can only be playing once at a time. So, if I have "laser_trill.au" looping and I try to play it again, the first, looping sound will stop. This happens even if I am playing it from a different AudioClip.

On the other hand, it is possible to have one sound looping in the background, and then play another sound on top of that, as long as they are two separate files. This is a common arrangement in games, where you have background music playing constantly, with occasional sound effects that overlap with the background music rather than interrupting it.

There is a limit to how many different sounds your computer can play at once; your sound card has only a certain number of "channels". This number varies from computer to computer, but is a large enough number (at least 16) that you don't really need to worry about it.