jmf问题!大家看看

zhuyue_csdn 2005-11-28 03:17:10
private String createProcessor() {
DataSource audioDS=null;
DataSource videoDS=null;
DataSource mergeDS=null;
StateListener stateListener=new StateListener();
//create the DataSource
//it can be a 'video' DataSource, an 'audio' DataSource
//or a combination of audio and video by merging both
if (videoLocator == null && audioLocator == null)
return "Locator is null";
if (audioLocator != null){
try {
//create the 'audio' DataSource
audioDS= javax.media.Manager.createDataSource(audioLocator);
} catch (Exception e) {
System.out.println("-> Couldn't connect to audio capture device");
}
}
if (videoLocator != null){
try {
//create the 'video' DataSource
videoDS = javax.media.Manager.createDataSource(videoLocator);
} catch (Exception e) {
System.out.println("-> Couldn't connect to video capture device");
}
}
if(videoDS!=null && audioDS!=null){
try {
//create the 'audio' and 'video' DataSource
mergeDS = javax.media.Manager.createMergingDataSource(new DataSource [] {audioDS, videoDS});
} catch (Exception e) {
System.out.println("-> Couldn't connect to audio or video capture device");
}
try{
//Create the processor from the merging DataSource
processor = javax.media.Manager.createProcessor(mergeDS);
}
catch (NoProcessorException npe) {
return "Couldn't create processor";
} catch (IOException ioe) {
return "IOException creating processor";
}
}
//if the processor has not been created from the merging DataSource
if(processor==null){
try {
if(audioDS!=null)
//Create the processor from the 'audio' DataSource
processor = javax.media.Manager.createProcessor(audioDS);
else
//Create the processor from the 'video' DataSource
processor = javax.media.Manager.createProcessor(videoDS);
} catch (NoProcessorException npe) {
return "Couldn't create processor";
} catch (IOException ioe) {
return "IOException creating processor";
}
}

// Wait for it to configure
boolean result = stateListener.waitForState(processor, Processor.Configured);
if (result == false)
return "Couldn't configure processor";

// Get the tracks from the processor
TrackControl [] tracks = processor.getTrackControls();

// Do we have atleast one track?
if (tracks == null || tracks.length < 1)
return "Couldn't find tracks in processor";

// Set the output content descriptor to RAW_RTP
// This will limit the supported formats reported from
// Track.getSupportedFormats to only valid RTP formats.
ContentDescriptor cd = new ContentDescriptor(ContentDescriptor.RAW_RTP);
processor.setContentDescriptor(cd);

Format supported[];
Format chosen=null;
boolean atLeastOneTrack = false;

// Program the tracks.
for (int i = 0; i < tracks.length; i++) {
chosen = null;
Format format = tracks[i].getFormat();
if (tracks[i].isEnabled()) {
/*if (tracks[i] instanceof VideoFormat)
System.out.println("Supported Video Formats :");
else
System.out.println("Supported Audio Formats :");*/
supported = tracks[i].getSupportedFormats();
/*System.out.println("track : "+ i);
for(int j=0;j<supported.length;j++)
System.out.println("Supported format : "+supported[j].getEncoding());*/
// We've set the output content to the RAW_RTP.
// So all the supported formats should work with RTP.

if (supported.length > 0) {
for(int j=0;j<supported.length;j++){
//System.out.println("Supported format : "+supported[j].toString().toLowerCase());
if (supported[j] instanceof VideoFormat) {
// For video formats, we should double check the
// sizes since not all formats work in all sizes.
if(sessionDescription.getVideoFormat()!=null)
if(supported[j].toString().toLowerCase().indexOf(
sessionDescription.getVideoFormat().toLowerCase())!=-1)
chosen = checkForVideoSizes(tracks[i].getFormat(),
supported[j]);
} else {
if(sessionDescription.getAudioFormat()!=null)
if(supported[j].toString().toLowerCase().indexOf(
sessionDescription.getAudioFormat().toLowerCase())!=-1)
chosen = supported[j];
}
}
if(chosen!=null){
tracks[i].setFormat(chosen);
System.err.println("Track " + i + " is set to transmit as:");
System.err.println(" " + chosen);
atLeastOneTrack = true;
}
} else
tracks[i].setEnabled(false);
} else
tracks[i].setEnabled(false);
}

if (!atLeastOneTrack)
return "Couldn't set any of the tracks to a valid RTP format";

// Realize the processor. This will internally create a flow
// graph and attempt to create an output datasource for JPEG/RTP
// audio frames.
/****************这里出错********************************************************/
result = stateListener.waitForState(processor, Controller.Realized);
/*******************************************************************************/
if (result == false)
return "Couldn't realize processor";

// Set the JPEG quality to .5.
setJPEGQuality(processor, 0.25f);

// Get the output data source of the processor
dataOutput = processor.getDataOutput();

return null;
}

public synchronized boolean waitForState(Processor p, int state) {
p.addControllerListener(this);
failed = false;

// Call the required method on the processor
if (state == Processor.Configured) {
p.configure();
} else if (state == Processor.Realized) {
p.realize();
}

// Wait until we get an event that confirms the
// success of the method, or a failure event.
// See StateListener inner class
while (p.getState() < state && !failed) {
synchronized (getStateLock()) {
try {
getStateLock().wait();
} catch (InterruptedException ie) {
return false;
}
}
}

if (failed)
return false;
else
return true;
}

到底哪里出现了问题
...全文
94 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
prok 2005-12-22
  • 打赏
  • 举报
回复
感觉是状态控制不对
JAVA多媒体开发手册,Java多媒体框架(JMF)中包含了许多用于处理多媒体的API。它是一个相当复杂的系统,完全了解这个系统可能需要花上几周的时间,但是这篇文章将主要介绍JMF的几个核心接口和类,然后通过一个简单的例子向你展示如何利用该接口进行编程。 JMF目前的最新版本是2.1,Sun通过它向Java中引入处理多媒体的能力。下面是JMF所支持的功能的一个概述: ● 可以在Java Applet和应用程序中播放各种媒体文件,例如AU、AVI、MIDI、MPEG、QuickTime和WAV等文件。 ● 可以播放从互联网上下载的媒体流。 ● 可以利用麦克风和摄像机一类的设备截取音频和视频,并保存成多媒体文件。 ● 处理多媒体文件,转换文件格式。 ● 向互联网上传音频和视频数据流。 ● 在互联网上广播音频和视频数据。 JMF的结构 为了更好地说明JMF的结构,让我们用立体声音响做一个简单的比喻。当你CD机播放CD唱片的时候,CD唱片向系统提供音乐信号。这些数据是在录音棚中用麦克风和其他类似的设备记录下来的。CD播放机将音乐信号传送到系统的音箱上。在这个例子中,麦克风就是一个音频截取设备,CD唱片是数据源,而音箱是输出设备。 JMF的结构和立体声音响系统非常相似,在后面的文章中,你会遇到下面的这些术语: ● 数据源(Data source) ● 截取设备(Capture Device,包括视频和音频截取设备) ● 播放器(Player) ● 处理器(Processor) ● 数据格式(Format) ● 管理器(Manager) 下面让我们来看一看这些术语到底代表什么意思。 1.数据源 就像CD中保存了歌曲一样,数据源中包含了媒体数据流。在JMF中,DataSource对象就是数据源,它可以是一个多媒体文件,也可以是从互联网上下载的数据流。对于DataSource对象,一旦你确定了它的位置和类型,对象中就包含了多媒体的位置信息和能够播放该多媒体的软件信息。当创建了DataSource对象后,可以将它送入Player对象中,而Player对象不需要关心DataSource中的多媒体是如何获得的,以及格式是什么。 在某些情况下,你需要将多个数据源合并成一个数据源。例如当你在制作一段录像时,你需要将音频数据源和视频数据源合并在一起。JMF支持数据源合并,在后面的例子中我们将提到这一点。 2.截取设备 截取设备指的是可以截取到音频或视频数据的硬件,如麦克风、摄像机等。截取到的数据可以被送入Player对象中进行处理。 3.播放器 在JMF中对应播放器的接口是Player。Player对象将音频/视频数据流作为输入,然后将数据流输出到音箱或屏幕上,就像CD播放机读取CD唱片中的歌曲,然后将信号送到音箱上一样。Player对象有多种状态,JMF中定义了JMF的六种状态,在正常情况下Player对象需要经历每个状态,然后才能播放多媒体。下面是对这些状态的说明。 ● Unrealized:在这种状态下,Player对象已经被实例化,但是并不知道它需要播放的多媒体的任何信息。 ● Realizing:当调用realize()方法时,Player对象的状态从Unrealized转变为Realizing。在这种状态下,Player对象正在确定它需要占用哪些资源。 ● Realized:在这种状态下Player对象已经确定了它需要哪些资源,并且也知道需要播放的多媒体的类型。 ● Prefetching:当调用prefectch()方法时,Player对象的状态从Realized变为Prefetching。在该状态下的Player对象正在为播放多媒体做一些准备工作,其中包括加载多媒体数据,获得需要独占的资源等。这个过程被称为预取(Prefetch)。 ● Prefetched:当Player对象完成了预取操作后就到达了该状态。 ● Started:当调用start()方法后,Player对象就进入了该状态并播放多媒体。 4.处理器 处理器对应的接口是Processor,它一种播放器。在JMF API中,Processor接口继承了Player接口。 Processor对象除了支持支持Player对象支持的所有功能,还可以控制对于输入的多媒体数据流进行何种处理以及通过数据源向其他的Player对象或Processor对象输出数据。 除了在播放器中提到了六种状态外,Processor 对象还包括两种新的状态,这两种状态是在Unrealized状态之后,但是在Realizing状态之前。 ● Configuring:当调用configure()方法后,Processor对象进入该状态。在该状态下

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧