26.07.2013 Views

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 22 <strong>Java</strong> Media Framework and <strong>Java</strong> Sound (on CD) 1261<br />

Formatting the DataSource completes the configuration of the capture device. A<br />

Processor (object inSource) converts the data <strong>to</strong> the file format in which it will be<br />

saved. The Processor works as a connec<strong>to</strong>r between the outSource and method<br />

captureSaveFile since DataSource inSource does not play or save the media,<br />

it only serves <strong>to</strong> configure the capture device. Line 157 invokes method captureSave-<br />

File (lines 265–322) <strong>to</strong> perform the steps needed <strong>to</strong> save the captured media in a recognizable<br />

file format.<br />

To create a Processor, this program first creates a ProcessorModel, a template<br />

for the Processor. The ProcessorModel determines the attributes of a Processor<br />

through a collection of information which includes a DataSource or MediaLoca<strong>to</strong>r,<br />

the desired formats for the media tracks which the Processor will handle, and a ContentDescrip<strong>to</strong>r<br />

indicating the output content type. Line 268 creates a new Format<br />

array (outFormats) that represents the possible formats for each track in the media. Line<br />

270 sets the default format <strong>to</strong> the first element of the array. To save the captured output <strong>to</strong><br />

a file, the Processor must first convert the data it receives <strong>to</strong> a file-enabled format. A<br />

new QuickTime FileTypeDescrip<strong>to</strong>r (package javax.media.format) is created<br />

<strong>to</strong> s<strong>to</strong>re a description of the content type of the Processor’s output and s<strong>to</strong>re it in<br />

outFileType (lines 273–274). Lines 282–284 use the DataSource inSource, the<br />

array outFormats, and the file type outFileType <strong>to</strong> instantiate a new Processor-<br />

Model (lines 283–284).<br />

Generally, Processors need <strong>to</strong> be configured before they can process media, but the<br />

one in this application does not since lines 282–284 invoke Manager method createRealizedProcessor.<br />

This method creates a configured, realized Processor based on the<br />

ProcessorModel object passed in as an argument. Method createRealizedProcessor<br />

throws a NoProcessorException if the program cannot locate a Processor<br />

for the media or if JMF does not support the media type. The method throws a CannotRealizeException<br />

if the Processor cannot be realized. This may occur if another program<br />

is already using the media, thus blocking communication with the media source.<br />

Common <strong>Program</strong>ming Error 22.1<br />

Be careful when specifying track formats. Incompatible formats for specific output file types<br />

prevent the program from realizing the Processor. 22.1<br />

Software Engineering Observation 22.2<br />

Recall that the Processor transitions through several states before being realized. Manager<br />

method createProcessor allows a program <strong>to</strong> provide more cus<strong>to</strong>mized configuration<br />

before a Processor is realized. 22.2<br />

Performance Tip 22.2<br />

When method createRealizedProcessor configures the Processor, the method<br />

blocks until the Processor is realized. This may prevent other parts of the program from<br />

executing. In some cases, using a ControllerListener <strong>to</strong> respond <strong>to</strong> Controller-<br />

Events may enable a program <strong>to</strong> operate more efficiently. When the Processor is realized,<br />

the listener is notified so the program can begin processing the media. 22.2<br />

Having obtained media data in a file format from the Processor, the program can<br />

make a “data writer” <strong>to</strong> write the media output <strong>to</strong> a file. An object that implements interface<br />

DataSink enables media data <strong>to</strong> be output <strong>to</strong> a specific location—most commonly a file.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!