Play a Sound with QuickTime for Java #73
Chapter 10, Audio
|
371
HACK
The one thing that’s really different is that you can play many more file for-
mats than you can with just JavaSound. Or maybe you can’t. It all depends
on what operating system you’re running on. The JMF install offers an all-
Java version and “performance packs” for some operating systems, which
use native code. The latter offer not only better performance for the media
formats supported by the all-Java version, they also integrate with the native
media library on the host operating system to play other media files, ones
that couldn’t be opened with the all-Java version alone.
This might make you deceive yourself: if you develop a JMF application on
Windows and it works with your media, it won’t necessarily work on
another operating system, either because Sun never made a performance
pack for that system (Mac OS X), or because even with the performance
pack, the native libraries don’t support that format. If you go the JMF route,
you need to rigorously test cross-platform, or you may very well have an
application that really only works on one OS.
H A C K
#73
Play a Sound with QuickTime for Java Hack #73
Using QuickTime, you can play even more kinds of sounds, but only on two
operating systems.
QuickTime for Java offers another way to significantly improve the media
capabilities of your application. Its list of supported formats is huge (see
http://www.apple.com/quicktime/products/qt/specifications.html for the current
list) and always growing as Apple continues to improve it. That’s a big
advantage over JMF, which was dropped into maintenance mode in 1999
and largely ignored since then.
The huge disadvantage with QuickTime for Java is that it works on Win-
dows and Mac only. That’s because QTJ, as it’s typically called, is really just
an object-oriented (OO) wrapper to call C functions in the native Quick-
Time library. That gives you native-speed performance, but it also means the
wrappers don’t do anything without an underlying native implementation.
QuickTime Beating Up on JavaSound
For the purposes of this hack, let’s say you only need to support Mac and
Windows, or that you need to open files from the iTunes Music Store (QTJ
can do it, which is apparently the only way to do it in Java), or for whatever
reason QTJ looks like the right solution. Example 10-5 shows a port of the
JavaSound audio player to a QTJ-based implementation.
372
|
Chapter 10, Audio
#73 Play a Sound with QuickTime for Java
HACK
Example 10-5. Playing audio with QuickTime for Java
import quicktime.std.*;
import quicktime.std.clocks.*;
import quicktime.std.movies.*;
import quicktime.*;
import quicktime.io.*;
import quicktime.app.time.*;
public class QTJSound extends Object {
File soundFile;
JDialog playingDialog;
Movie movie;
public static void main (String[] args) {
JFileChooser chooser = new JFileChooser( );
chooser.showOpenDialog(null);
File f = chooser.getSelectedFile( );
try {
QTJSound s = new QTJSound (f);
} catch (Exception e) {
e.printStackTrace( );
}
}
public QTJSound (File f)
throws QTException {
soundFile = f;
// prepare a dialog to display while playing
JOptionPane pane = new JOptionPane ("Playing " + f.getName( ),
JOptionPane.PLAIN_MESSAGE);
playingDialog = pane.createDialog (null, "QTJ Sound");
playingDialog.pack( );
// get and play sound
QTSession.open( );
QTFile qtf = new QTFile (f);
OpenMovieFile omf = OpenMovieFile.asRead (qtf);
movie = Movie.fromFile (omf);
MyDemoCloser closer = new MyDemoCloser (movie);
TaskAllMovies.addMovieAndStart ( );
movie.start( );
playingDialog.setVisible(true);
}
class MyDemoCloser extends ExtremesCallBack {
public MyDemoCloser (Movie m) throws QTException {
super (m.getTimeBase( ),
StdQTConstants.triggerAtStop);
callMeWhen( );
}

Get Swing Hacks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.