| CONTENTS | PREV | NEXT | INDEX | JMF 2.0 API Guide |
You can use JMF to capture media data from a capture device such as a microphone or video camera. Captured media data can be processed and rendered or stored for future use.
- Locate the capture device you want to use by querying the
CaptureDeviceManager.- Get a
CaptureDeviceInfoobject for the device.- Get a
MediaLocatorfrom theCaptureDeviceInfoobject and use it to create aDataSource.- Create a
PlayerorProcessorusing theDataSource.- Start the
PlayerorProcessorto begin the capture process.When you use a capture
DataSourcewith aPlayer, you can only render the captured media data. To explicitly process or store the captured media data, you need to use aProcessor.Accessing Capture Devices
You access capture devices through the
CaptureDeviceManager. TheCaptureDeviceManageris the central registry for all of the capture devices available to JMF. You can get a list of the available capture devices by calling theCaptureDeviceManager.getDeviceListmethod.Each device is represented by a
CaptureDeviceInfoobject. To get theCaptureDeviceInfoobject for a particular device, you callCaptureDeviceManager.getDevice:CaptureDeviceInfo deviceInfo = CaptureDeviceManager.getDevice("deviceName");Capturing Media Data
To capture media data from a particular device, you need to get the device's
MediaLocatorfrom itsCaptureDeviceInfoobject. You can either use thisMediaLocatorto construct aPlayerorProcessordirectly, or use theMediaLocatorto construct aDataSourcethat you can use as the input to aPlayerorProcessor. To initiate the capture process, youstartthePlayerorProcessor.Allowing the User to Control the Capture Process
A capture device generally has a set of implementation-specific attributes that can be used to control the device. Two control types are defined to enable programmatic control of capture devices:
PortControlandMonitorControl. You access these controls by callinggetControlon the captureDataSourceand passing in the name of the control you want.A
PortControlprovides a way to select the port from which data will be captured. AMonitorControlprovides a means for displaying the device's capture monitor.Like other
Controlobjects, if there's a visual component that corresponds to thePortControlorMonitorControl, you can get it by callinggetControlComponent. Adding theComponentto your applet or application window will enable users to interact with the capture control.You can also display the standard control-panel component and visual component associated with the
PlayerorProcessoryou're using.
Storing Captured Media Data
If you want to save captured media data to a file, you need to use a
Processorinstead of aPlayer. You use aDataSinkto read media data fromProcessorobject's output data source and render the data to a file.
- Get the output
DataSourcefrom theProcessorby callinggetDataOutput.- Construct a file writer
DataSinkby callingManager.createDataSink.Pass in the outputDataSourceand aMediaLocatorthat specifies the location of the file to which you want to write.- Call
openon theDataSinkto open the file.- Call
starton theDataSink.- Call
starton theProcessorto begin capturing data.- Wait for an
EndOfMediaEvent, a particular media time, or a user event.- Call
stopon theProcessorto end the data capture.- Call
closeon theProcessor.- When the
Processoris closed and theDataSinkposts anEndOfStreamEvent, callcloseon theDataSink.
Example: Capturing and Playing Live Audio Data
To capture live audio data from a microphone and present it, you need to:
- Get the
CaptureDeviceInfoobject for the microphone.- Create a
Playerusing theMediaLocatorretrieved from theCaptureDeviceInfoobject. (You can create thePlayerby callingcreatePlayer(MediaLocator)or create aDataSourcewith theMediaLocatorand usecreatePlayer(DataSource)to construct thePlayer.)
Example: Writing Captured Audio Data to a File
You can write captured media data to a file using a
DataSink. To capture and store audio data, you need to:
- Get a
CaptureDeviceInfoobject for the audio capture device.- Create a
Processorusing theMediaLocatorretrieved from theCaptureDeviceInfoobject.- Get the output
DataSourcefrom theProcessor.- Create a
MediaLocatorfor the file where you want to write the captured data.- Create a file writer
DataSinkusing the outputDataSource.- Start the file writer and the
Processor.This example uses a helper class,
StateHelper.java, to manage the state of theProcessor. The complete source forStateHelperis included in the appendix starting on page 179.
Example: Encoding Captured Audio Data
You can configure a
Processorto transcode captured media data before presenting, transmitting, or storing the data. To encode captured audio data in the IMA4 format before saving it to a file:
- Get the
MediaLocatorfor the capture device and construct aProcessor.- Call
configureon theProcessor.- Once the
Processoris in the Configured state, callgetTrackControls.- Call
setFormaton each track until you find one that can be converted to IMA4. (ForsetFormatto succeed, appropriate codec plug-ins must be available to perform the conversion.)- Realize the
Processorand use it's outputDataSourceto construct aDataSinkto write the data to a file.
Example: Capturing and Saving Audio and Video Data
In this example, a
ProcessorModelis used to create aProcessorto capture live audio and video data, encode the data as IMA4 and Cinepak tracks, interleave the tracks, and save the interleaved media stream to a QuickTime file.When you construct a
ProcessorModelby specifying the track formats and output content type and then use that model to construct aProcessor, theProcessoris automatically connected to the capture device that meets the format requirements, if there is one.