Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
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 theImageIcon
to aJButton
. When you press the button, a WAV file is played. TheSoundApplet
code was modified to only contain this button. The resulting applet is calledPictButton
. Here are the files you will need: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.
PictButton.java
AppletSoundList.java
AppletSoundLoader.java
(to load the sound)- Sample image and sample sound:
rocketship.gif
andbottle-open.wav
.- Sample HTML page:
PictButton.html
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:
- Make sure that your WAV file and code are in the same directory.
- If you are running an applet, add the name of your sound to
SoundApplet.java
like this (otherwise, add it toSoundApplication.java
):If you look atString wavFile = "bottle-open.wav";SoundApplet.java
orSoundApplication.java
, you will see the names of our sound files listed in this way near the top of the code.- Make sure that you have
SoundApplet.java
,AppletSoundList.java
andAppletSoundLoader.java
and be sure that you compile them together like this:If you are running an application, make sure that you havejavac *javaSoundApplication.java
,SoundList.java
, andSoundLoader.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 inSoundApplet.java
andSoundApplication.java
in the methodstartLoadingSounds()
.
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
Feedback Form |
Copyright 1995-2001 Sun Microsystems, Inc. All rights reserved.