| Log In | Not a Member? |
Contact ADC
|
|
![]() |
|
|
| Previous Section | Table of Contents | Next Section |
Module 4- Playing sounds using QuickTime for JavaContents Overview In this module, we will explore the wonderful world of audio and learn how to play sounds of various formats using QuickTime for Java. On creation of the main window, we will load a sound file into memory and play it back. This module expands upon the framework that was set in place in the previous modules. Introduction to Playing Sounds in QTJ QuickTime for Java provides rich support for various sound file formats. Java's built-in audio support is surprisingly poor- support is limited to wav or au files and it is very difficult to control the playback rate of the sound unless you know in advance how many samples per second the sound file uses. QuickTime for Java, on the other hand, supports a myriad of popular file formats including (but not limited to the following:
Playback of audio media is reasonably straight-forward and can be integrated easily into non-QuickTime applications. In this module we will examine how to use QuickTime to play sounds. This project is based on the previous module and assumes that you have completed it. If you would like to download a completed copy of the module 3, you may do so. We have renamed the main class to "Zoo4" and changed all instances of previous module classes to prevent confusion. Other than, we do not need to modify the main class file. Click here to see the main source file Zoo4.java. Importing Packages and Declaring Data Members All of our source modification will occur in our AnimalPane source file. First, we add additional packages we need to import:
Since we just want to play a sound and don't want to give the user any playback control, we will use a QTPlayer simply to handle the playing of the sound data. Additionally, we will not be adding the QTPlayer to the compositor because we have no visual representation of our sound. Next we will learn how to load sounds into memory for subsequent playback. In order to playback a sound file, we need to retrieve it from disk and load it into memory:
This method is placed at the beginning of the constructor so that we can load the sound immediately, before any graphics are drawn and before the compositor is created. This will allow us to play the sound asynchronously as our graphics are being drawn. Towards the bottom of our file, we implement the loadSound( ) method. We declare it protected because we only want it accessible to this file and its subclasses. The method takes a String parameter that represents the path to the sound file to load. Now let's implement this method. First, we get the path to the file by using the QTFactory utility class to generate a URL from a local file path. This location is simply a string. It does not need to refer to a local file. It can be the URL of a file located on the internet and served over ftp, http, or via an rtsp streaming server. This gives us great flexibility by dealing with remote and local files in the same manner. Next, we create a drawable object from the string. This may seem counter-intuitive since we are dealing with a sound and not a movie. This is because QuickTime treats sound and media files, as well as other time-based formats, in a consistent manner. By making a drawable object, we are preparing the file to be presented by the QTPlayer. Although we are not using a controller, we could easily do so by adding our QTPlayer to our compositor. Finally, we add a try/catch block to handle any exceptions that are thrown. As always, the standard caveat applies here about error handling and how "real" applications should do better error checking and gracefully recover from problems. Now that we have the sound loaded, it is time to play the sound:
We write a public routine called playSound( ) that takes no parameters. The first thing we do is check the player to make sure it is not null. If it is, we have no sound to play, so we return from the routine. Otherwise, we create a new try block and call the setTime( ) method on the player. This will "rewind" the sound and set the playback position to the beginning of the sound file. Thus, if the QTPlayer is already playing this sound, and we call playSound( ) again, the sound will be rewound and restarted. Then, we call the startTasking( ) of the QTPlayer inherited from the Taskable class. This creates a task that is responsible for handling the periodic data. If we do not do make this call, the sound will not play correctly. Finally, we start the sound playing by setting the rate to 1 via the setRate( ) call. This call is the same one we used in module 2 to start the playback of the movie. This completes the source code modifications to module 4. It's time to run your application and see the results. If you would like to see the entire source file of either Zoo4.java, or AnimalPane.java please follow the appropriate links. This module demonstrated how to use QuickTime's QTPlayer class to playback audio files. We chose this methodology to play sound files because it is fairly robust, allows files to be located on disk, on the web, or streamed, and supports a variety of sound file formats including AIFF, wav, au, and MP3. Although we chose not to have a visual representation for our sound, we discussed how to give the user a controller for the sound by adding the component to the compositor. In our next module we will learn how to control the playback of media by creating a custom controller. The clever reader will realize quickly that the information that was presented here only scratches the surface of what could be done using QuickTime for Java. Although we will expand this example in future modules, there are few things that you could do on your own as exploration that will help you better understand how QuickTime for Java works. Each one of these examples has a source code solution, so if you have trouble, feel free to consult it. Experiment with one or more of the following:
|
| Previous Section | Table of Contents | Next Section |