Capturing Audio to Disk

Typically, you don’t just capture media and immediately dispose of it—you want to save the media to disk as you capture so that you can use it later. Fortunately, the SequenceGrabber makes this pretty easy.

How do I do that?

Adding to the previous labs’ code, the calls to set up the SequenceGrabber need to be changed to prepare for grabbing to disk. Specifically, the SGSoundChannel ’s setUsage() call gets a flag to indicate that it will be writing the captured audio to disk:

soundChannel.setUsage (StdQTConstants.seqGrabPreview |
                     StdQTConstants.seqGrabRecord);

Next, add a call to give the user an opportunity to configure the audio capture:

soundChannel.settingsDialog( );

Warning

The settingsDialog( ) call will crash Java 1.4.2 on Mac OS X if called from the AWT event-dispatch thread. Yes, it’s a bug. To work around this until the bug is fixed, you can stash the call in another thread and block on it. For instance, in this example you could replace the settingsDialog( ) call with the following:

final SGSoundChannel sc = soundChannel;
Thread t = new Thread( ) {
public void run( ) {
try {
sc.settingsDialog( );
} catch (QTException qte) {
qte.printStackTrace( );
}
}
};
t.start( );
while (t.isAlive( ))
Thread.yield( );

After starting the preview, tell the SequenceGrabber where it should save the captured audio:

// create output file grabFile = new QTFile (new java.io.File ("audiograb.mov")); if (grabFile.exists( )) grabFile.delete( ); grabber.setDataOutput(grabFile, ...

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.