1. 捕获媒体数据的步骤:
(1) 查询CaptureDeviceManager,来定位你需要使用的媒体采集设备。
(2) 得到此设备的CaptureDeviceInfo实例。
(3) 从此CaptureDeviceInfo实例获得一个MediaLocator,并通过它来创建一个DataSource。
(4)用此DataSource创建一个Player或Processor。
(5) 启动此Player或Processor开始捕获媒体数据。
2.CaptureDeviceManager、CaptureDeviceInfo、MediaLocator
在JMF中,CaptureDeviceManager也是一个manager类,它提供给了一个列表,这个列表显示当前系统可以被使用的设备名称。同时CaptureDeviceManager可以通过查询的方法对设备进行定位并返回设备的配置信息对象CaptureDeviceInfo,它也可以通过注册的方法向列表加入一个新的设备信息,以便为JMF使用。
设备可通过CaptureDeviceManager的getDevice()方法直接获得设备控制权,设备的控制权一旦得到,就可以以此设备作为一个MediaLocator,可以通过CaptureDeviceInfo的getLocator()方法得到。
3.JMF识别的音频采集设备
4.一个实例实现音频捕获
实例有两个文件组成。CaptureDemo.java实现
①查询、获得音频采集设备。
②捕获音频。
③将音频保存到本地文件foo.wav。
StateHelper实现处理器(processor)的状态控制管理。以下为流程图:
![200704301177922812069.jpg](https://i-blog.csdnimg.cn/blog_migrate/0e46c6fdd05e7e059d992b859707b0c5.jpeg)
importjavax.media.CaptureDeviceInfo;
importjavax.media.CaptureDeviceManager;
importjavax.media.DataSink;
importjavax.media.Manager;
importjavax.media.MediaLocator;
importjavax.media.NoDataSinkException;
importjavax.media.NoProcessorException;
importjavax.media.Processor;
importjavax.media.control.StreamWriterControl;
importjavax.media.format.AudioFormat;
importjavax.media.protocol.DataSource;
importjavax.media.protocol.FileTypeDescriptor;
public class CaptureDemo
{
/* *
*@paramargs
*/
public static void main(String[]args)
{
// TODOAuto-generatedmethodstub
CaptureDeviceInfodi = null ;
Processorp = null ;
StateHelpersh = null ;
VectordevList = CaptureDeviceManager.getDeviceList( new AudioFormat( " linear " , 44100 , 16 , 2 ));
if (devList.size() > 0 )
{
di = (CaptureDeviceInfo)devList.firstElement();
System. out .println(di.getName());
System. out .println(di.toString());
}
else
System.exit( - 1 );
try
{
p = Manager.createProcessor(di.getLocator());
sh = new StateHelper(p);
}
catch (java.io.IOExceptionex)
{
System.exit( - 1 );
}
catch (NoProcessorExceptione)
{
// TODOAuto-generatedcatchblock
System.exit( - 1 );
}
if ( ! sh.configure( 10000 ))
{
System.exit( - 1 );
}
p.setContentDescriptor( new FileTypeDescriptor(FileTypeDescriptor.WAVE));
if ( ! sh.realize( 10000 ))
{
System.exit( - 1 );
}
DataSourcesource = p.getDataOutput();
MediaLocatordest = new MediaLocator( " file:///c:/foo.wav " );
DataSinkfileWriter = null ;
try
{
fileWriter = Manager.createDataSink(source,dest);
fileWriter.open();
}
catch (NoDataSinkExceptione)
{
// TODOAuto-generatedcatchblock
System.exit( - 1 );
}
catch (java.io.IOExceptionex)
{
System.exit( - 1 );
}
catch (SecurityExceptionex)
{
System.exit( - 1 );
}
StreamWriterControlswc = (StreamWriterControl)p.getControl( " javax.media.control.StreamWriterControl " );
if (swc != null )
{
swc.setStreamSizeLimit( 500000000 );
}
try
{
fileWriter.start();
}
catch (java.io.IOExceptionex)
{
System.exit( - 1 );
}
sh.playToEndOfMedia( 5000 * 1000 );
sh.close();
fileWriter.close();
}
}
importjavax.media.ControllerClosedEvent;
importjavax.media.ControllerErrorEvent;
importjavax.media.ControllerEvent;
importjavax.media.ControllerListener;
importjavax.media.EndOfMediaEvent;
importjavax.media.Player;
importjavax.media.Processor;
importjavax.media.RealizeCompleteEvent;
public class StateHelperimplementsControllerListener
{
Playerplayer = null ;
booleanconfigured = false ;
booleanrealized = false ;
booleanprefetched = false ;
booleanendOfMedia = false ;
booleanfailed = false ;
booleanclosed = false ;
public StateHelper(Playerp)
{
this .player = p;
p.addControllerListener( this );
}
public booleanconfigure( int timeOutMillis)
{
long startTime = System.currentTimeMillis();
synchronized( this )
{
if ( this .playerinstanceofProcessor)
{
((Processor)player).configure();
}
else
return false ;
while ( ! this .configured &&! this .failed)
{
try
{
this .wait(timeOutMillis);
}
catch (InterruptedExceptionex)
{
}
if (System.currentTimeMillis() - startTime > timeOutMillis)
break ;
}
}
return this .configured;
}
public booleanrealize( int timeOutMillis)
{
long startTime = System.currentTimeMillis();
synchronized( this )
{
this .player.realize();
while ( ! this .realized &&! this .failed)
{
try
{
this .wait(timeOutMillis);
}
catch (InterruptedExceptionex)
{
}
if (System.currentTimeMillis() - startTime > timeOutMillis)
break ;
}
}
return this .realized;
}
public booleanprefetch( int timeOutMillis)
{
long startTime = System.currentTimeMillis();
synchronized( this )
{
this .player.prefetch();
while ( ! this .prefetched &&! this .failed)
{
try
{
this .wait(timeOutMillis);
}
catch (InterruptedExceptionex)
{
}
if (System.currentTimeMillis() - startTime > timeOutMillis)
break ;
}
}
return this .prefetched &&! this .failed;
}
public booleanplayToEndOfMedia( int timeOutMillis)
{
long startTime = System.currentTimeMillis();
this .endOfMedia = false ;
synchronized( this )
{
this .player.start();
while ( ! this .endOfMedia &&! this .failed)
{
try
{
this .wait(timeOutMillis);
}
catch (InterruptedExceptionex)
{
}
if (System.currentTimeMillis() - startTime > timeOutMillis)
break ;
}
}
return this .endOfMedia &&! this .failed;
}
public void close()
{
synchronized( this )
{
this .player.close();
while ( ! this .closed)
{
try
{
this .wait( 100 );
}
catch (InterruptedExceptionex)
{
}
}
}
this .player.removeControllerListener( this );
}
public synchronized void controllerUpdate(ControllerEventce)
{
// TODOAuto-generatedmethodstub
if (ceinstanceofRealizeCompleteEvent)
{
this .realized = true ;
}
else if (ceinstanceofConfigureCompleteEvent)
{
this .configured = true ;
}
else if (ceinstanceofEndOfMediaEvent)
{
this .endOfMedia = true ;
}
else if (ceinstanceofControllerErrorEvent)
{
this .failed = true ;
}
else if (ceinstanceofControllerClosedEvent)
{
this .closed = true ;
}
else
{
return ;
}
this .notifyAll();
}
}