📄 jmfcapturing.html
字号:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<meta name="GENERATOR" content="Quadralay WebWorks Publisher 5.0.2">
<meta name="TEMPLATEBASE" content="Portable HTML">
<meta name="LASTUPDATED" content="11/23/99 11:48:06">
<title>Capturing Time-Based Media with JMF </title>
</head>
<body link="#3366CC" vlink="#9999CC" text="#000000" alink="#0000CC" bgcolor="#FFFFFF"
background="images/backgrnd.gif">
<table width="100%" border="0" align="left" cellpadding="0" cellspacing="0">
<tr>
<td><a href="JMFTOC.html">CONTENTS</a> |
<a href="JMFProcessing.html">PREV </a> |
<a href="JMFExtending.html">NEXT</a> |
<a href="JMFIX.html">INDEX</a></td>
<td align="right"><em>JMF 2.0 API Guide</em>
</tr>
</table>
<p><br clear="all">
</p>
<hr align="left">
<blockquote>
<div align="right">
<a name="98228"> </a><font size="3" face="Palatino, Times New Roman, Times, serif">5 <br></font>
</div>
<div align="right">
<h2>
<a name="98540"> </a><font color="#003366" face="Palatino, Times New Roman, Times, serif">Capturing Time-Based Media with JMF </font>
</h2>
</div>
<p>
<a name="95098"> </a><font face="Palatino, Times New Roman, Times, serif">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. </font>
</p>
<p>
<a name="95103"> </a><font face="Palatino, Times New Roman, Times, serif">To capture media data, you:</font>
</p>
<ul>
<li><a name="95100"> </a><font face="Palatino, Times New Roman, Times, serif">Locate the capture device you want to use by querying the <code>CaptureDeviceManager</code>.</font>
<li value="2"><a name="95067"> </a><font face="Palatino, Times New Roman, Times, serif">Get a <code>CaptureDeviceInfo</code> object for the device.</font>
<li value="3"><a name="95159"> </a><font face="Palatino, Times New Roman, Times, serif">Get a <code>MediaLocator</code> from the <code>CaptureDeviceInfo</code> object and use it to create a <code>DataSource</code>.</font>
<li value="4"><a name="95160"> </a><font face="Palatino, Times New Roman, Times, serif">Create a <code>Player</code> or <code>Processor</code> using the <code>DataSource</code>.</font>
<li value="5"><a name="95161"> </a><font face="Palatino, Times New Roman, Times, serif">Start the <code>Player</code> or <code>Processor</code> to begin the capture process.</font>
</ul>
<p>
<a name="95177"> </a><font face="Palatino, Times New Roman, Times, serif">When you use a capture <code>DataSource</code> with a <code>Player</code>, you can only render the captured media data. To explicitly process or store the captured media data, you need to use a <code>Processor</code>.</font>
</p>
<h3>
<a name="95168"> </a><font color="#003366" face="Palatino, Times New Roman, Times, serif">Accessing Capture Devices</font>
</h3>
<p>
<a name="98338"> </a><font face="Palatino, Times New Roman, Times, serif">You access capture devices through the <code>CaptureDeviceManager</code>. The <code>CaptureDeviceManager</code> is the central registry for all of the capture devices available to JMF. You can get a list of the available capture devices by calling the <code>CaptureDeviceManager</code>.<code>getDeviceList</code> method.</font>
</p>
<p>
<a name="98340"> </a><font face="Palatino, Times New Roman, Times, serif">Each device is represented by a <code>CaptureDeviceInfo</code> object. To get the <code>CaptureDeviceInfo</code> object for a particular device, you call <code>CaptureDeviceManager.getDevice</code>: </font>
</p>
<pre>
<a name="94960"> </a><code>CaptureDeviceInfo deviceInfo = CaptureDeviceManager.getDevice("deviceName");
</code></pre>
<h3>
<a name="95169"> </a><font color="#003366" face="Palatino, Times New Roman, Times, serif">Capturing Media Data</font>
</h3>
<p>
<a name="94961"> </a><font face="Palatino, Times New Roman, Times, serif">To capture media data from a particular device, you need to get the device's <code>MediaLocator</code> from its <code>CaptureDeviceInfo</code> object. You can either use this <code>MediaLocator</code> to construct a <code>Player</code> or <code>Processor</code> directly, or use the <code>MediaLocator</code> to construct a <code>DataSource</code> that you can use as the input to a <code>Player</code> or <code>Processor</code>. To initiate the capture process, you <code>start </code>the <code>Player </code>or <code>Processor</code>.</font>
</p>
<h3>
<a name="95146"> </a><font color="#003366" face="Palatino, Times New Roman, Times, serif">Allowing the User to Control the Capture Process</font>
</h3>
<p>
<a name="94982"> </a><font face="Palatino, Times New Roman, Times, serif">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: <code>PortControl</code> and <code>MonitorControl</code>. You access these controls by calling <code>getControl</code> on the capture <code>DataSource</code> and passing in the name of the control you want.</font>
</p>
<p>
<a name="95237"> </a><font face="Palatino, Times New Roman, Times, serif">A <code>PortControl</code> provides a way to select the port from which data will be captured. A <code>MonitorControl</code> provides a means for displaying the device's capture monitor.</font>
</p>
<p>
<a name="95252"> </a><font face="Palatino, Times New Roman, Times, serif">Like other <code>Control</code> objects, if there's a visual component that corresponds to the <code>PortControl</code> or <code>MonitorControl</code>, you can get it by calling <code>getControlComponent</code>. Adding the <code>Component</code> to your applet or application window will enable users to interact with the capture control.</font>
</p>
<p>
<a name="95251"> </a><font face="Palatino, Times New Roman, Times, serif">You can also display the standard control-panel component and visual component associated with the <code>Player</code> or <code>Processor</code> you're using. </font>
</p>
<a name="99175"> </a><font size="1" face="Palatino, Times New Roman, Times, serif">
<table border="1" bordercolorlight="#FFFFFF" bordercolordark="#000000"
cellpadding="5" cellspacing="0">
<caption><b><i><font face="Palatino, Times New Roman, Times, serif"><a name="99181"> </a>Example 5-1: Displaying GUI components for a processor.</font></i></b></caption>
<tr>
<td><font face="Palatino, Times New Roman, Times, serif"><pre>
<a name="99185"> </a><code>Component controlPanel, visualComponent;
</code><a name="99186"> </a><code>if ((controlPanel = p.getControlPanelComponent()) != null)
</code><a name="99187"> </a><code> add(controlPanel);
</code><a name="99188"> </a><code>if ((visualComponent = p.getVisualComponent()) != null)
</code><a name="99189"> </a><code> add(visualComponent); </code>
</pre>
</font></td>
</tr>
</table>
<br></font>
<h3>
<a name="96582"> </a><font color="#003366" face="Palatino, Times New Roman, Times, serif">Storing Captured Media Data</font>
</h3>
<p>
<a name="96584"> </a><font face="Palatino, Times New Roman, Times, serif">If you want to save captured media data to a file, you need to use a <code>Processor</code> instead of a <code>Player</code>. You use a <code>DataSink</code> to read media data from <code>Processor </code>object's output data source and render the data to a file. </font>
</p>
<ol type="1">
<li value="1"><a name="100227"> </a><font face="Palatino, Times New Roman, Times, serif">Get the output <code>DataSource</code> from the <code>Processor</code> by calling <code>getDataOutput.</code></font>
<li value="2"><a name="100228"> </a><font face="Palatino, Times New Roman, Times, serif">Construct a file writer <code>DataSink</code> by calling <code>Manager.createDataSink. </code>Pass in the output <code>DataSource</code> and a <code>MediaLocator</code> that specifies the location of the file to which you want to write.</font>
<li value="3"><a name="100229"> </a><font face="Palatino, Times New Roman, Times, serif">Call <code>open</code> on the <code>DataSink</code> to open the file.</font>
<li value="4"><a name="100230"> </a><font face="Palatino, Times New Roman, Times, serif">Call <code>start</code> on the <code>DataSink</code>.</font>
<li value="5"><a name="100235"> </a><font face="Palatino, Times New Roman, Times, serif">Call <code>start</code> on the <code>Processor</code> to begin capturing data.</font>
<li value="6"><a name="100236"> </a><font face="Palatino, Times New Roman, Times, serif">Wait for an <code>EndOfMediaEvent</code>, a particular media time, or a user event.</font>
<li value="7"><a name="100239"> </a><font face="Palatino, Times New Roman, Times, serif">Call <code>stop</code> on the <code>Processor</code> to end the data capture.</font>
<li value="8"><a name="100240"> </a><font face="Palatino, Times New Roman, Times, serif">Call <code>close</code> on the <code>Processor</code>.</font>
<li value="9"><a name="100241"> </a><font face="Palatino, Times New Roman, Times, serif">When the <code>Processor</code> is closed and the <code>DataSink</code> posts an <code>EndOfStreamEvent</code>, call <code>close</code> on the <code>DataSink</code>.</font>
</ol>
<a name="99203"> </a><font size="1" face="Palatino, Times New Roman, Times, serif">
<table border="1" bordercolorlight="#FFFFFF" bordercolordark="#000000"
cellpadding="5" cellspacing="0">
<caption><b><i><font face="Palatino, Times New Roman, Times, serif"><a name="99209"> </a>Example 5-2: Saving captured media data to a file.</font></i></b></caption>
<tr>
<td><font face="Palatino, Times New Roman, Times, serif"><pre>
<a name="99213"> </a><code>DataSink sink;
</code><a name="99214"> </a><code>MediaLocator dest = new MediaLocator("file://newfile.wav");
</code><a name="99215"> </a><code>try {
</code><a name="99216"> </a><code> sink = Manager.createDataSink(p.getDataOutput(), dest);
</code><a name="99217"> </a><code> sink.open();
</code><a name="99218"> </a><code> sink.start();
</code><a name="99219"> </a><code>} catch (Exception) {}
</code></pre>
</font></td>
</tr>
</table>
<br></font>
<h3>
<a name="93820"> </a><font color="#003366" face="Palatino, Times New Roman, Times, serif">Example: Capturing and Playing Live Audio Data</font>
</h3>
<p>
<a name="94097"> </a><font face="Palatino, Times New Roman, Times, serif">To capture live audio data from a microphone and present it, you need to:</font>
</p>
<ol type="1">
<li value="1"><a name="98820"> </a><font face="Palatino, Times New Roman, Times, serif">Get the <code>CaptureDeviceInfo</code> object for the microphone.</font>
<li value="2"><a name="98868"> </a><font face="Palatino, Times New Roman, Times, serif">Create a <code>Player</code> using the <code>MediaLocator</code> retrieved from the <code>CaptureDeviceInfo</code> object. (You can create the <code>Player</code> by calling <code>createPlayer(MediaLocator)</code> or create a <code>DataSource</code> with the <code>MediaLocator</code> and use <code>createPlayer(DataSource)</code> to construct the <code>Player</code>.)</font>
</ol>
<a name="99228"> </a><font size="1" face="Palatino, Times New Roman, Times, serif">
<table border="1" bordercolorlight="#FFFFFF" bordercolordark="#000000"
cellpadding="5" cellspacing="0">
<caption><b><i><font face="Palatino, Times New Roman, Times, serif"><a name="99234"> </a>Example 5-3: Capturing and playing audio from a microphone.</font></i></b></caption>
<tr>
<td><font face="Palatino, Times New Roman, Times, serif"><pre>
<a name="99238"> </a><code>// Get the CaptureDeviceInfo for the live audio capture device
</code><a name="99239"> </a><code>Vector deviceList = CaptureDeviceManager.getDeviceList(new
</code><a name="99240"> </a><code> AudioFormat("linear", 44100, 16, 2));
</code><a name="99241"> </a><code>if (deviceList.size() > 0)
</code><a name="99242"> </a><code> di = (CaptureDeviceInfo)deviceList.firstElement();
</code><a name="99243"> </a><code>else
</code><a name="99244"> </a><code>// Exit if we can't find a device that does linear, 44100Hz, 16 bit,
// stereo audio.
</code><a name="100202"> </a><code> System.exit(-1);
</code><a name="100203"> </a><code>
</code><a name="100204"> </a><code>// Create a Player for the capture device:
</code><a name="99248"> </a><code>try{
</code><a name="99249"> </a><code> Player p = Manager.createPlayer(di.getLocator());
</code><a name="99250"> </a><code>} catch (IOException e) {
</code><a name="99251"> </a><code>} catch (NoPlayerException e) {}
</code></pre>
</font></td>
</tr>
</table>
<br></font>
<h3>
<a name="93821"> </a><font color="#003366" face="Palatino, Times New Roman, Times, serif">Example: Writing Captured Audio Data to a File</font>
</h3>
<p>
<a name="98268"> </a><font face="Palatino, Times New Roman, Times, serif">You can write captured media data to a file using a <code>DataSink</code>. To capture and store audio data, you need to:</font>
</p>
<ol type="1">
<li value="1"><a name="98890"> </a><font face="Palatino, Times New Roman, Times, serif">Get a <code>CaptureDeviceInfo</code> object for the audio capture device.</font>
<li value="2"><a name="98894"> </a><font face="Palatino, Times New Roman, Times, serif">Create a <code>Processor</code> using the <code>MediaLocator</code> retrieved from the <code>CaptureDeviceInfo</code> object.</font>
<li value="3"><a name="98895"> </a><font face="Palatino, Times New Roman, Times, serif">Get the output <code>DataSource</code> from the <code>Processor</code>.</font>
<li value="4"><a name="98987"> </a><font face="Palatino, Times New Roman, Times, serif">Create a <code>MediaLocator</code> for the file where you want to write the captured data.</font>
<li value="5"><a name="98896"> </a><font face="Palatino, Times New Roman, Times, serif">Create a file writer <code>DataSink</code> using the output <code>DataSource</code>.</font>
<li value="6"><a name="98995"> </a><font face="Palatino, Times New Roman, Times, serif">Start the file writer and the <code>Processor</code>.</font>
</ol>
<p>
<a name="100735"> </a><font face="Palatino, Times New Roman, Times, serif">This example uses a helper class, <code>StateHelper.java</code>, to manage the state of the <code>Processor</code>. The complete source for <code>StateHelper</code> is included in the appendix starting on <a href="JMFApp-StateHelper.html#85027">page 179</a>.</font>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -