The JavaTM Tutorial

| Start of Tutorial > Start of Trail > Start of Lesson | Search

Trail: Sound

## Solving Common Sound Problems

> Problem: How can I have an WAV or MP3 file play when an image on my Web page is clicked?
> - To have a sound play when an image is clicked, your picture must be contained in a component that registers mouse events. The following sample adds a GIF to an `ImageIcon` and adds the `ImageIcon` to a `JButton`. When you press the button, a WAV file is played. The `SoundApplet` code was modified to only contain this button. The resulting applet is called `PictButton`. Here are the files you will need:
>   - `PictButton.java`
>   - `AppletSoundList.java`
>   - `AppletSoundLoader.java` (to load the sound)
>   - Sample image and sample sound: `rocketship.gif` and `bottle-open.wav`.
>   - Sample HTML page: `PictButton.html`
>   You must have the Java Plug-in 1.2 to run this sample on your browser and you should use the provided sample HTML file because it contains instructions that will point to the plug-in.
> Problem: Does the Java Platform support audio recording?
> - Java 2 (JDK 1.2) does not include support for audio recording. This functionality will be provided through the Java Sound API and the Java Media Framework (JMF) 2.0 API. The Java Sound API and a preliminary implementation will be available soon. The JMF 2.0 API is currently available for public review. For more information, see the Java Media web pages.

>   > Java Sound: `http://java.sun.com/products/java-media/sound/`
>   > Java Media Framework: `http://java.sun.com/products/java-media/jmf/`

> Problem: Is there a way to play a WAV file that does not have an URL?
> - If you have a file on your system, you can use the code in the tutorial to generate the URL. All that you need to do is:
>   1. Make sure that your WAV file and code are in the same directory.
>   2. If you are running an applet, add the name of your sound to `SoundApplet.java` like this (otherwise, add it to `SoundApplication.java`):

>      >             String wavFile = "bottle-open.wav";

>      If you look at `SoundApplet.java` or `SoundApplication.java`, you will see the names of our sound files listed in this way near the top of the code.
>   3. Make sure that you have `SoundApplet.java`, `AppletSoundList.java` and `AppletSoundLoader.java` and be sure that you compile them together like this:

>      >          	javac *java

>      If you are running an application, make sure that you have `SoundApplication.java`, `SoundList.java`, and `SoundLoader.java`

>   The URL is generated by the code with the filename that you give it. If you are running the applet code, the base URL is retrieved with `getCodeBase()`. The base URL is basically the path indicating where your file is located. For example, if your file is located at `/mymachine/java/samples/sound` then this would be your base URL.

>   If you are running the application code, the base URL is retrieved with `System.getProperty("user.dir")`. In both cases, the filename is attached to the base URL while using the startLoading method. You can see an example of this in `SoundApplet.java` and `SoundApplication.java` in the method `startLoadingSounds()`.

> Problem: I want to play several sound files in succession from an applet. However, when I use play two times after each other they just get mixed up.
> - Unfortunately, the Java 2 SDK does not provide notification of when playback has completed. The Java Sound API provides this feature. It is available as an early access release. If you must use the SDK, you could try to determine the length of each clip and sleep for the same about of time before playing the next clip.

| Start of Tutorial > Start of Trail > Start of Lesson | Search