COMP 202: Principles of Object-Oriented Programming II← The View and Conroller → |
Home — Fall 2008 | | | Information | | | Resources | | | Exam Resources | | | OwlSpace | | | COMP 201 |
Use the techiques below to create the Announcer class, which should have obvious usage in the HangmanFrame's win() and lose() methods.
Java can play either AU or WAV files.
Be sure to put your audio files in the appropriate package under the "classes" directory!
Simply call
Toolkit.getDefaultToolkit().beep();
The Toolkit is a collection of useful methods for the AWT. Since it is system dependent, the static getDefaultToolkit() method returns the toolkit appropriate for the program's run-time environment. It is a static Toolkit factory.
Java supports the following audio file types for creating an AudioClip:
Let "applet" be a reference to the applet itself:
AudioClip myAudioClip = applet.getAudioClip(applet.getCodeBase(),"myAudioFile.au"); myAudioClip.play();
This only works from inside an applet.
Use the Applet's static methods to bring in an audio file. Note that the exception must be caught or thrown. The [soundfile directory] is relative to the default directory.
AudioClip soundClip; File file = new File([sound file directory] + [soundfile]); try { soundClip = Applet.newAudioClip(file.toURL()); } catch(MalformedURLException mfe) { }
To play the sound:
soundClip.play();
When running the application from inside JBuilder, the sound file directory is relative to the JBuilder working directory. On the other hand, when running the application from the command line, the sound file directory is relative to the default Java directory. If you have not set the the JBuilder working directory to be the "classes" sub-directory, then the program will not run under both JBuilder and the command line.
URL: http://www.cs.rice.edu/teaching/202/08-fall/hw/hangman/sounds.shtml
Copyright © 2008-2010 Mathias Ricken and Stephen Wong