Listening for Movie State-Changes

One problem with polling to show the current time in the movie is that it’s wasteful and inaccurate: it’s optimal to check the time only when the movie’s playing, and to eliminate latency, it would be nice to be notified when there’s a sudden change in the current time, such as when the user slides the scrubber. Fortunately, there’s a callback API to notify a program when things like this occur.

How do I do that?

This example revises the BasicQTButtons program. The new version, BasicQTCallback, asks to be notified when the rate changes. When the rate is 0, it will disable the stop button (labeled “0”), and when the rate is 1, it disables the play button (labeled “1”). For space, I’ll list only the lines that have changed from BasicQTButtons.

First, there are two new imports: quicktime.std.clocks, which is where callbacks are defined, and quicktime.std, whose StdQTConstants provides constants to specify the callbacks’ behavior:

import quicktime.std.*;
import quicktime.std.clocks.*;

Next, the constructor is changed to pass the Movie to an inner class’ constructor:

MyQTCallback myCallback = new MyQTCallback (m);

And here’s the inner class. It has a constructor that takes a Movie argument and an execute( ) method:

class MyQTCallback extends RateCallBack { public MyQTCallback (Movie m) throws QTException { super (m.getTimeBase( ), 0, StdQTConstants.triggerRateChange); callMeWhen( ); } public void execute( ) { if (rateWhenCalled = = 0.0) { startButton.setEnabled ...

Get QuickTime for Java: A Developer's Notebook 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.