📄 jmfcapturing.html
字号:
</p>
<a name="99270"> </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="99276"> </a>Example 5-4: Writing captured audio to a file with a DataSink. (1 of 2)</font></i></b></caption>
<tr>
<td><font face="Palatino, Times New Roman, Times, serif"><pre>
<a name="100813"> </a> CaptureDeviceInfo di = null;
<a name="100814"> </a> Processor p = null;
<a name="100815"> </a> StateHelper sh = null;
<a name="100816"> </a> Vector deviceList = CaptureDeviceManager.getDeviceList(new
<a name="100817"> </a> AudioFormat(AudioFormat.LINEAR, 44100, 16, 2));
<a name="100818"> </a> if (deviceList.size() > 0)
<a name="100819"> </a> di = (CaptureDeviceInfo)deviceList.firstElement();
<a name="100820"> </a> else
<a name="100821"> </a> // Exit if we can't find a device that does linear,
<a name="101039"> </a> // 44100Hz, 16 bit,
<a name="100822"> </a> // stereo audio.
<a name="100823"> </a> System.exit(-1);
<a name="100824"> </a> try {
<a name="100825"> </a> p = Manager.createProcessor(di.getLocator());
<a name="100826"> </a> sh = new StateHelper(p);
<a name="100827"> </a> } catch (IOException e) {
<a name="100828"> </a> System.exit(-1);
<a name="100829"> </a> } catch (NoProcessorException e) {
<a name="100830"> </a> System.exit(-1);
<a name="100831"> </a> }
<a name="100832"> </a> // Configure the processor
<a name="100833"> </a> if (!sh.configure(10000))
<a name="100834"> </a> System.exit(-1);
<a name="100835"> </a> // Set the output content type and realize the processor
<a name="100836"> </a> p.setContentDescriptor(new
<a name="101040"> </a> FileTypeDescriptor(FileTypeDescriptor.WAVE));
<a name="100837"> </a> if (!sh.realize(10000))
<a name="100838"> </a> System.exit(-1);
<a name="100839"> </a> // get the output of the processor
<a name="100840"> </a> DataSource source = p.getDataOutput();
<a name="100841"> </a> // create a File protocol MediaLocator with the location of the
<a name="100842"> </a> // file to which the data is to be written
<a name="100843"> </a> MediaLocator dest = new MediaLocator("file://foo.wav");
<a name="100844"> </a> // create a datasink to do the file writing & open the sink to
<a name="100845"> </a> // make sure we can write to it.
<a name="100846"> </a> DataSink filewriter = null;
<a name="100847"> </a> try {
<a name="100848"> </a> filewriter = Manager.createDataSink(source, dest);
<a name="100849"> </a> filewriter.open();
<a name="100850"> </a> } catch (NoDataSinkException e) {
<a name="100851"> </a> System.exit(-1);
<a name="100852"> </a> } catch (IOException e) {
<a name="100853"> </a> System.exit(-1);
<a name="100854"> </a> } catch (SecurityException e) {
<a name="100855"> </a> System.exit(-1);
<a name="100484"> </a> }
</pre>
</font></td>
</tr>
<tr>
<td><font face="Palatino, Times New Roman, Times, serif"><pre>
<a name="100860"> </a><code> // if the Processor implements StreamWriterControl, we can
</code><a name="100861"> </a><code> // call setStreamSizeLimit
</code><a name="100862"> </a><code> // to set a limit on the size of the file that is written.
</code><a name="100863"> </a><code> StreamWriterControl swc = (StreamWriterControl)
</code><a name="100864"> </a><code> p.getControl("javax.media.control.StreamWriterControl");
</code><a name="100865"> </a><code> //set limit to 5MB
</code><a name="100866"> </a><code> if (swc != null)
</code><a name="101034"> </a><code> swc.setStreamSizeLimit(5000000);
</code><a name="101035"> </a><code>
</code><a name="101036"> </a><code> // now start the filewriter and processor
</code><a name="100870"> </a><code> try {
</code><a name="100871"> </a><code> filewriter.start();
</code><a name="100872"> </a><code> } catch (IOException e) {
</code><a name="100873"> </a><code> System.exit(-1);
</code><a name="100874"> </a><code> }
</code><a name="100875"> </a><code> // Capture for 5 seconds
</code><a name="100876"> </a><code> sh.playToEndOfMedia(5000);
</code><a name="100877"> </a><code> sh.close();
</code><a name="100878"> </a><code> // Wait for an EndOfStream from the DataSink and close it...
</code><a name="99339"> </a><code> filewriter.close();
</code></pre>
</font></td>
</tr>
</table>
<br></font>
<h3>
<a name="95728"> </a><font color="#003366" face="Palatino, Times New Roman, Times, serif">Example: Encoding Captured Audio Data</font>
</h3>
<p>
<a name="94296"> </a><font face="Palatino, Times New Roman, Times, serif">You can configure a <code>Processor</code> to 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:</font>
</p>
<ol type="1">
<li value="1"><a name="99054"> </a><font face="Palatino, Times New Roman, Times, serif">Get the <code>MediaLocator</code> for the capture device and construct a <code>Processor</code>.</font>
<li value="2"><a name="99055"> </a><font face="Palatino, Times New Roman, Times, serif">Call <code>configure</code> on the <code>Processor</code>.</font>
<li value="3"><a name="99056"> </a><font face="Palatino, Times New Roman, Times, serif">Once the <code>Processor</code> is in the <em>Configured</em> state, call <code>getTrackControls</code>.</font>
<li value="4"><a name="99097"> </a><font face="Palatino, Times New Roman, Times, serif">Call <code>setFormat</code> on each track until you find one that can be converted to IMA4. (For <code>setFormat</code> to succeed, appropriate codec plug-ins must be available to perform the conversion.)</font>
<li value="5"><a name="99035"> </a><font face="Palatino, Times New Roman, Times, serif">Realize the <code>Processor</code> and use it's output <code>DataSource</code> to construct a <code>DataSink</code> to write the data to a file.</font>
</ol>
<a name="97968"> </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="100577"> </a>Example 5-5: Encoding captured audio data. </font></i></b></caption>
<tr>
<td><font face="Palatino, Times New Roman, Times, serif"><pre>
<a name="100920"> </a><code> // Configure the processor
</code><a name="100921"> </a><code> if (!sh.configure(10000))
</code><a name="100922"> </a><code> System.exit(-1);
</code><a name="100923"> </a><code> // Set the output content type
</code><a name="100924"> </a><code> p.setContentDescriptor(new
FileTypeDescriptor(FileTypeDescriptor.WAVE));
</code><a name="100925"> </a><code>
</code><a name="100926"> </a><code> // Get the track control objects
</code><a name="100927"> </a><code> TrackControl track[] = p.getTrackControls();
</code><a name="100928"> </a><code> boolean encodingPossible = false;
</code><a name="100929"> </a><code> // Go through the tracks and try to program one of them
</code><a name="100930"> </a><code> // to output ima4 data.
</code><a name="100931"> </a><code> for (int i = 0; i < track.length; i++) {
</code><a name="100932"> </a><code> try {
</code><a name="100933"> </a><code> track[i].setFormat(new AudioFormat(AudioFormat.IMA4_MS));
</code><a name="100934"> </a><code> encodingPossible = true;
</code><a name="100935"> </a><code> } catch (Exception e) {
</code><a name="100936"> </a><code> // cannot convert to ima4
</code><a name="100937"> </a><code> track[i].setEnabled(false);
</code><a name="100938"> </a><code> }
</code><a name="100939"> </a><code> }
</code><a name="100940"> </a><code>
</code><a name="100941"> </a><code> if (!encodingPossible) {
</code><a name="100942"> </a><code> sh.close();
</code><a name="100943"> </a><code> System.exit(-1);
</code><a name="100944"> </a><code> }
</code><a name="100945"> </a><code> // Realize the processor
</code><a name="100946"> </a><code> if (!sh.realize(10000))
</code><a name="100947"> </a><code> System.exit(-1);
</code></pre>
</font></td>
</tr>
</table>
<br></font>
<h3>
<a name="99943"> </a><font color="#003366" face="Palatino, Times New Roman, Times, serif">Example: Capturing and Saving Audio and Video Data</font>
</h3>
<p>
<a name="99944"> </a><font face="Palatino, Times New Roman, Times, serif">In this example, a <code>ProcessorModel</code> is used to create a <code>Processor</code> to 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.</font>
</p>
<p>
<a name="99965"> </a><font face="Palatino, Times New Roman, Times, serif">When you construct a <code>ProcessorModel</code> by specifying the track formats and output content type and then use that model to construct a <code>Processor</code>, the <code>Processor</code> is automatically connected to the capture device that meets the format requirements, if there is one.</font>
</p>
<a name="99941"> </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="100066"> </a>Example 5-6: Creating a capture Processor with ProcessorModel. </font></i></b></caption>
<tr>
<td><font face="Palatino, Times New Roman, Times, serif"><pre>
<a name="100979"> </a> Format formats[] = new Format[2];
<a name="100980"> </a> formats[0] = new AudioFormat(AudioFormat.IMA4);
<a name="100981"> </a> formats[1] = new VideoFormat(VideoFormat.CINEPAK);
<a name="100982"> </a> FileTypeDescriptor outputType =
<a name="100983"> </a> new FileTypeDescriptor(FileTypeDescriptor.QUICKTIME);
<a name="100984"> </a> Processor p = null;
<a name="100985"> </a>
<a name="100986"> </a> try {
<a name="100987"> </a> p = Manager.createRealizedProcessor(new ProcessorModel(formats,
<a name="100988"> </a> outputType));
<a name="100989"> </a> } catch (IOException e) {
<a name="100990"> </a> System.exit(-1);
<a name="100991"> </a> } catch (NoProcessorException e) {
<a name="100992"> </a> System.exit(-1);
<a name="100993"> </a> } catch (CannotRealizeException e) {
<a name="100994"> </a> System.exit(-1);
<a name="100995"> </a> }
<a name="100996"> </a> // get the output of the processor
<a name="100997"> </a> DataSource source = p.getDataOutput();
<a name="100998"> </a> // create a File protocol MediaLocator with the location
<a name="101041"> </a> // of the file to
<a name="100999"> </a> // which bits are to be written
<a name="101000"> </a> MediaLocator dest = new MediaLocator("file://foo.mov");
<a name="101001"> </a> // create a datasink to do the file writing & open the
<a name="101042"> </a> // sink to make sure
<a name="101002"> </a> // we can write to it.
<a name="101003"> </a> DataSink filewriter = null;
<a name="101004"> </a> try {
<a name="101005"> </a> filewriter = Manager.createDataSink(source, dest);
<a name="101006"> </a> filewriter.open();
<a name="101007"> </a> } catch (NoDataSinkException e) {
<a name="101008"> </a> System.exit(-1);
<a name="101009"> </a> } catch (IOException e) {
<a name="101010"> </a> System.exit(-1);
<a name="101011"> </a> } catch (SecurityException e) {
<a name="101012"> </a> System.exit(-1);
<a name="101013"> </a> }
<a name="101014"> </a> // now start the filewriter and processor
<a name="101015"> </a> try {
<a name="101016"> </a> filewriter.start();
<a name="101017"> </a> } catch (IOException e) {
<a name="101018"> </a> System.exit(-1);
<a name="101019"> </a> }
<a name="101020"> </a> p.start();
<a name="101021"> </a> // stop and close the processor when done capturing...
<a name="100766"> </a> // close the datasink when EndOfStream event is received...
</pre>
</font></td>
</tr>
</table>
<br></font>
</blockquote>
<br clear="all">
<hr>
<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>
<br>
<hr>
<em>
<a href="copyright.html">Copyright</a> ©
1998-1999 Sun Microsystems, Inc. All Rights Reserved.
</em>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -