Chapter 10, Audio
#73 Play a Sound with QuickTime for Java
example, if the media could play in both directions and you wanted to be
notified when it reached the beginning of the movie too, you’d pass
StdQTConstants.triggerAtStop | StdQTConstants.triggerAtStart
Next, you have to use
to register the callback. This signs you
up for one callback—if you’re called and are still interested in future events,
you have to reregister with another
When the sound finishes, the callback calls
execute( ). This is when you shut
everything down, as in the other hacks. Notice that you close down Quick-
QTSession.close( ), the obvious counterpart to QTSession.open( ).
There are some issues about how well it works on Windows: it sometimes
hangs for me, and you may want to use the safer
which only closes down some of QTJ, but the rest seems to get taken care of
by QTJ itself, as I’ve never had a problem.
And one more bit of arcane QTJ lore: the code makes a call to
TaskAllMovies.addMovieAndStart( ). This helps deal with the fact that mov-
ies have to explicitly be given CPU time, with calls to a
task( ) method, in
order to work.
TaskAllMovies is a convenience Thread that can periodically
make this tasking call for all your movies. If you’ve read Chris’ book on
QTJ, you would think that this isn’t necessary, as having the AWT event-
dispatch thread usually provides tasking calls. The problem is that the dia-
log box that’s showing while the audio plays is modal; thus, it blocks the
event loop, which in turn blocks the tasking you usually get for free with
AWT. So, you have to set it up yourself.
QTJ is full of weird gotchas like this. What do you expect
when it’s largely a port from C?
Compiling QuickTime Code
Yep, this hack has special compile instructions. First, you have to be sure
that your machine even has QuickTime for Java on it. It’s installed by
default with Mac OS X, so this is only an issue for Windows-based develop-
ers. On Windows, if you don’t have QuickTime at all, get it from http://
www.apple.com/quicktime/ and do a custom install: QuickTime for Java will
be one of the non-default optional pieces, and you just need to checkmark it
to include it in your install. If you do have QuickTime, run the QuickTime
Updater from your Start menu or your tray to do a “custom” update, which
will show the same list of optional pieces as the main installer.