JMF捕获音频

1. 捕获媒体数据的步骤:

(1) 查询CaptureDeviceManager,来定位你需要使用的媒体采集设备。

(2) 得到此设备的CaptureDeviceInfo实例。

(3) 从此CaptureDeviceInfo实例获得一个MediaLocator,并通过它来创建一个DataSource

(4)用此DataSource创建一个PlayerProcessor

(5) 启动此PlayerProcessor开始捕获媒体数据。

2CaptureDeviceManagerCaptureDeviceInfoMediaLocator

JMF中,CaptureDeviceManager也是一个manager,它提供给了一个列表,这个列表显示当前系统可以被使用的设备名称。同时CaptureDeviceManager可以通过查询的方法对设备进行定位并返回设备的配置信息对象CaptureDeviceInfo,它也可以通过注册的方法向列表加入一个新的设备信息,以便为JMF使用。

设备可通过CaptureDeviceManagergetDevice()方法直接获得设备控制权,设备的控制权一旦得到,就可以以此设备作为一个MediaLocator,可以通过CaptureDeviceInfogetLocator()方法得到。

3JMF识别的音频采集设备

200792401.jpg

4.一个实例实现音频捕获

实例有两个文件组成。CaptureDemo.java实现

查询、获得音频采集设备。

捕获音频。

将音频保存到本地文件foo.wav

StateHelper实现处理器(processor)的状态控制管理。以下为流程图:

200704301177922812069.jpg

<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--> importjava.util.Vector;
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();
}

}


<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--> importjavax.media.ConfigureCompleteEvent;
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();
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值