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

Control iTunes Under Windows #83
Chapter 11, Native Integration and Packaging
|
423
HACK
Compile this class along with the interfaces in the
test.jtunes
package. You
will also need the com4j.jar in your
CLASSPATH
and com4j.dll files in your
PATH. When you run the program, the com4j library will connect to
iTunes—launching it if necessary—and execute the
playPause( ) method.
com4j only allows you to call methods from the same thread
that you used to create the COM proxy. Typically, you want
to update your Swing components with iTunes information,
which you can do safely from the Swing event thread only.
This means that you should create the COM proxy from the
event thread as well (using the
ClassFactory method). Unfor-
tunately, this may cause your application to block for a few
seconds while iTunes loads (if it’s not already running). To
avoid this delay, you probably want to do all of your iTunes
communication through a custom queue, or use the new
concurrency utilities available in Java 5.0. The com4j devel-
opers are working on a solution to this problem, so it may be
solved by the time you read this.
Get Track Information
As with Apple Events on the Mac [Hack #82], the COM interface gives you a
way to query the currently playing track. You can call
iTunes.currentTrack( )
to get an IITTrack object. This object has methods to query just about any-
thing you could possible want to know about a track, including the artist,
album, playing time, encoding method, and even the import date. Each
method on the
IITTrack object returns information as Strings or Java primi-
tives, so it’s pretty easy to access anything you want and then stuff it into
your Swing interface. The following code shows how to get the track num-
ber, count, name, album, and artist:
IITTrack track;
track = itunes.currentTrack( );
int track_number = track.trackNumber( );
int track_count = track.trackCount( );
IiTunes itunes;
itunes = ClassFactory.createiTunesApp( );
itunes.playPause( );
} catch (Exception ex) {
System.out.println("exception : " + ex.getMessage( ));
ex.printStackTrace( );
}
}
}
Example 11-6. A listener to control iTunes on Windows (continued)

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