Browse DevX
Sign up for e-mail newsletters from DevX


Load Images and Play Sounds in Java Applets  : Page 4

Embed Java applets that load images and play sounds into your Web pages.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Add a Soundtrack
Now you're ready for something more complex—adding a soundtrack to play in the background of your applet at the same time. You can see the code in Listing 5 (the HTML code is in Listing 6).

As you learned previously, multiple AudioClip objects can play at the same time. So, simply make another AudioClip object that loads your soundtrack. Have it start playing right in the applet's init() method since it doesn't need to be fired by a specific event (like a mouse click) but rather play from the time the applet is loaded.

In fact, because you want the soundtrack to play continually, play it with the AudioClip's loop() method. As you might expect, loop() just plays the sound over and over again, but it has an unexpected catch. The sound will indeed play over and over—even when you leave the Web page containing your applet!

Let's put a stop to that behavior! Open a few new Web browser windows (using CTRL-N or New Browser Window from the File menu). Now, open the HTML file in Listing 6, which loads your Java class. Your soundtrack plays and the applet works as expected. Now tell your browser to load, say, http://www.devx.com/. The applet no longer runs, but the jingle keeps playing! Even when you close that browser window, the music doesn't stop. You have to close every browser window before the tune actually stops.

This is the only real "gotcha" with the AudioClip object. Make sure you tell the looping sound to stop when your applet closes (using the stop() method). Fortunately, the stop() method is very easy to use. Listing 7 shows you how to do it (see Listing 8 for HTML). The Applet class provides stop(), just as it provides init() and paint(). The program calls this method whenever the applet should close down, whether because the user has moved to a new page or the Web browser is being closed or any other reason. So, in your applet, just call the stop() method of the soundtrack AudioClip object.

Actually, your chosen sound file may prove to be grating to visitors to your Web site anyway, so you may want to provide an "on/off" toggle button. Simply add a button and use a boolean variable to specify whether the soundtrack is playing or not (see Listing 9 for Java code and Listing 10 for HTML). If it is playing, then you stop it; if it is not, then you start it looping again.

Applets in Action
That's about all there is to adding images and sound to your applets! Click the following links to see—and hear—some examples that demonstrate how your applets will work:

  • Simple Java Applet
  • Simple Java Applet with Audio
  • Simple Java Applet with Looping Audio That StopsWhen User Moves to New Web Page
  • Simple Java Applet with Toggle Button for Turning Soundtrack On and Off

  • David M. Williams is an Australian I.T. writer, trainer, and consultant. He is the author of Win32 Font Lister and other shareware programs. Reach him by e-mail at dave@qed.newcastle.edu.au.
    Comment and Contribute






    (Maximum characters: 1200). You have 1200 characters left.



    Thanks for your registration, follow us on our social networks to keep up-to-date