O'Reilly logo

Swing Hacks by Chris Adamson, Joshua Marinacci

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

374
|
Chapter 10, Audio
#73 Play a Sound with QuickTime for Java
HACK
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
callMeWhen( )
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
callMeWhen( )
.
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-
Time with
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
QTSession.exitMovies( ),
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.
Play a Sound with QuickTime for Java #73
Chapter 10, Audio
|
375
HACK
The install or update will put QTJava.zip into the lib/ext of any Java home
folders it finds. It should also put a copy in C:\windows\system32 and add a
system-wide environment variable
QTJAVA
pointing to one of these files, with
the path in quotes (which may or may not be good for you, depending on
what else you do with environment variables).
Here’s the fun part about compiling: you must explicitly point your compile-
time classpath to one of these QTJava.zip files for
javac (or jikes, or what-
ever) to find the QTJ classes. If you don’t, you’ll get a bunch of compile-
time errors like:
QTJSound.java:4: package quicktime.std does not exist
import quicktime.std.*;
^
QTJSound.java:5: package quicktime.std.clocks does not exist
import quicktime.std.clocks.*;
^
QTJSound.java:6: package quicktime.std.movies does not exist
import quicktime.std.movies.*;
So, assuming you want to work with a copy of Java installed in Program
Files\Java\j2re1.4.2_06 (which is my current path…yours may vary), you
compile this demo with the following command:
C:\>javac
-classpath "c:\Program Files\Java\j2re1.4.2_06\lib\ext\QTJava.zip"
QTJSound.java
This should be typed as one line—I’ve word-wrapped to
accommodate the book’s margins.
You can substitute any other path to a QTJava.zip file for the classpath if
you find it more convenient, or just write an Ant build file to automate
everything for you.
On Mac OS X, the location of QTJava.zip is never a mystery because the sys-
tem installer puts it in /System/Library/Java/Extensions.QTJava.zip. So, you
compile with:
[aeris:HacksBook/Media/52] cadamson% javac -classpath
/System/Library/Java/Extensions/QTJava.zip
QTJSound.java
In either case, you’ll probably be relieved to know that you don’t have to
specify the classpath when running a QTJ application.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required