`
dato0123
  • 浏览: 916290 次
文章分类
社区版块
存档分类
最新评论

JMF示例(二)

 
阅读更多
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
importjavax.media.
*;
importcom.sun.media.ui.
*;
importjavax.media.protocol.
*;
importjavax.media.protocol.DataSource;
importjavax.swing.
*;
importjavax.swing.
event.*;
importjava.awt.
*;
importjava.awt.
event.*;
importjava.net.
*;
importjava.io.
*;
importjava.util.Vector;

publicclassMDIAppextendsFrame
{

/*************************************************************************
*MAINPROGRAM/STATICMETHODS
************************************************************************
*/
publicstaticvoidmain(Stringargs[])
{
MDIAppmdi
=newMDIApp();
}
staticvoidFatal(Strings)
{
MessageBoxmb
=newMessageBox("JMFError",s);
}
/*************************************************************************
*VARIABLES
************************************************************************
*/
JMFramejmframe
=null;
JDesktopPanedesktop;
FileDialogfd
=null;
CheckboxMenuItemcbAutoLoop
=null;
Playerplayer
=null;
PlayernewPlayer
=null;
Stringfilename;

/*************************************************************************
*METHODS
************************************************************************
*/

publicMDIApp()
{
super(
"JavaMediaPlayer");
//Addthedesktoppane
setLayout(newBorderLayout());
desktop
=newJDesktopPane();
desktop.setDoubleBuffered(
true);//设置双缓存
add("Center",desktop);
setMenuBar(createMenuBar());
setSize(
640,480);
setVisible(
true);
try
{
UIManager.setLookAndFeel(
"javax.swing.plaf.metal.MetalLookAndFeel");
}
catch(Exceptione)
{
System.err.println(
"Couldnotinitializejava.awtMetallnf");
}
addWindowListener(
newWindowAdapter(){
publicvoidwindowClosing(WindowEventwe){
System.exit(
0);
}
});
Manager.setHint(Manager.LIGHTWEIGHT_RENDERER,
newBoolean(true));
}

privateMenuBarcreateMenuBar()
{
ActionListeneral
=newActionListener()
{
publicvoidactionPerformed(ActionEventae)
{
Stringcommand
=ae.getActionCommand();
if(command.equals("Open"))
{
if(fd==null)
{
fd
=newFileDialog(MDIApp.this,"OpenFile",
FileDialog.LOAD);
fd.setDirectory(
"/movies");
}
fd.show();
if(fd.getFile()!=null)
{
Stringfilename
=fd.getDirectory()+fd.getFile();
openFile(
"file:"+filename);
}
}
elseif(command.equals("Exit"))
{
dispose();
System.exit(
0);
}
}
};

MenuItemitem;
MenuBarmb
=newMenuBar();
//FileMenu
MenumnFile=newMenu("File");
mnFile.add(item
=newMenuItem("Open"));
item.addActionListener(al);
mnFile.add(item
=newMenuItem("Exit"));
item.addActionListener(al);

//OptionsMenu
MenumnOptions=newMenu("Options");
cbAutoLoop
=newCheckboxMenuItem("Autoreplay");
cbAutoLoop.setState(
true);
mnOptions.add(cbAutoLoop);

mb.add(mnFile);
mb.add(mnOptions);
returnmb;
}

/**
*Openamediafile.
*/
publicvoidopenFile(Stringfilename)
{
StringmediaFile
=filename;
Playerplayer
=null;
//URLforourmediafile
URLurl=null;
try
{
//Createanurlfromthefilenameandtheurltothe
//documentcontainingthisapplet.
if((url=newURL(mediaFile))==null)
{
Fatal(
"Can'tbuildURLfor"+mediaFile);
return;
}

//Createaninstanceofaplayerforthismedia
try
{
player
=Manager.createPlayer(url);//创建播放器
}
catch(NoPlayerExceptione)
{
Fatal(
"Error:"+e);
}
}
catch(MalformedURLExceptione)
{
Fatal(
"Error:"+e);
}
catch(IOExceptione)
{
Fatal(
"Error:"+e);
}
if(player!=null)
{
this.filename=filename;//保存文件名称
JMFramejmframe=newJMFrame(player,filename);//新建立一个播放窗口
desktop.add(jmframe);
}
}
}

classJMFrameextendsJInternalFrameimplementsControllerListener
{
//播放器
Playermplayer;
Componentvisual
=null;
Componentcontrol
=null;
intvideoWidth=0;
intvideoHeight=0;
intcontrolHeight=30;
intinsetWidth=10;
intinsetHeight=30;
booleanfirstTime
=true;

publicJMFrame(Playerplayer,Stringtitle)
{
super(title,
true,true,true,true);
getContentPane().setLayout(
newBorderLayout());
setSize(
320,10);
setLocation(
50,50);
setVisible(
true);
mplayer
=player;
mplayer.addControllerListener((ControllerListener)
this);
mplayer.realize();
addInternalFrameListener(
newInternalFrameAdapter()
{
publicvoidinternalFrameClosing(InternalFrameEventife)
{
mplayer.close();
}
}
);
}

publicvoidcontrollerUpdate(ControllerEventce)
{
if(ceinstanceofRealizeCompleteEvent)
{
mplayer.prefetch();
}
elseif(ceinstanceofPrefetchCompleteEvent)
{
if(visual!=null)
return;

if((visual=mplayer.getVisualComponent())!=null)
{
Dimensionsize
=visual.getPreferredSize();
videoWidth
=size.width;
videoHeight
=size.height;
getContentPane().add(
"Center",visual);
}
else
videoWidth
=320;
if((control=mplayer.getControlPanelComponent())!=null)
{
controlHeight
=control.getPreferredSize().height;
getContentPane().add(
"South",control);
}
setSize(videoWidth
+insetWidth,videoHeight+controlHeight+insetHeight);
validate();
mplayer.start();
}
elseif(ceinstanceofEndOfMediaEvent)
{
mplayer.setMediaTime(
newTime(0));
mplayer.start();
}
}
}

问题:如何结合Swing组件使用JMFSwing组件是轻量级组件,而JMF默认使用的是重量级组件(它们可以为高速率的视频使用本地的绘制方法)。

解决方案:

JMF2.0包含了几个不同的视频绘制器,可以强制让播放器使用轻量级组件。示例如下:

Manager.setHint(Manager.LIGHTWEIGHT_RENDERER, new Boolean(true));

这在内部是通过PlugInManager来使其只能使用轻量级绘制器,此后创建的任何播放器都将使用轻量级绘制器。注意的是存在父容器中的组件的双缓存功能应该要开启。

分享到:
评论

相关推荐

    java的JMF播放音乐示例

    可以搜索E盘里所有的MP3文件,将其播放,只要在控制台输入数字,就可以播放音乐,是学习java的JMF的简单示例。代码可运行。

    Jmf捕获摄像头的示例

    Jmf捕获摄像头的示例

    JMF摄像头视频采集示例

    给学员写的一个JMF的例子,能捕获本机摄像头的视频并在窗体口中显示。 严格来说还不完整,因为原本打算实现网络传输的,后来改成保存到磁盘文件了。呵呵。

    利用JMF进行多媒体编程

    Java多媒体框架(JMF)中包含了许多用于处理多媒体的API。它是一个相当复杂的系统,完全了解这个系统可能需要花上几周的时间,但是这篇文章将主要介绍JMF的几个核心接口和类,然后通过一个简单的例子向你展示如何...

    Java编写多个爬虫实例

    VApplet和vid2jpg JMF示例 pageSim 计算网页相似度 SST 计算网站风格树 cobra 基于视觉的网页分块算法 HTIS HTIS算法 PageRank PageRank算法 Link 链接 WebGraph Web图建模 WebGraphMemory 内存Web图 Synonym 同义词...

    JMF_Fina_2020

    这是基于R Markdown和bookdown ( )的书籍的最小示例。 请参阅上的“”页面,以了解如何将此示例编译为HTML。 您可以通过调用bookdown::render_book('index.Rmd', 'bookdown::pdf_book')生成bookdown::pdf_book格式...

    北大MOOC《Java程序设计》源码

    这是我在学习中国大学MOOC北京大学唐大仕老师的《Java程序设计》的过程中写的代码,其中,LearnJava里的代码是我个人写的,PKU-MOOC-JavaExample里有课程PDF和示例代码。 其中用到的Java库有:httpcomponents-...

    Java开发技术大全 电子版

    7.5.2RandomAccessFile类使用示例——文件加密256 7.6控制台输入和输出257 7.6.1控制台输入类Scanner257 7.6.2格式化输出printf258 7.7序列化261 7.8本章小结264 第4篇Java中的高级技术 第8章Java的多线程...

    JimToMov(图片转视频)

    压缩包中包括使用示例,图片资源,JimToMov.jar,jmf-2_1_1e-alljava.zip框架jar包

    JAVA基础课程讲义

    目 录 第一章 JAVA入门 10 计算机语言发展史 10 机器语言 10 汇编语言 10 高级语言 10 其他高级语言 11 JAVA发展简史 12 JAVA为什么能够流行?...JAVA各版本的含义 13 ...Java运行时环境JRE(Java ...代码示例和效果 233

    Java拍照功能

     保存图像的就不多说了,以下为示例代码 bufferedimage bi = (bufferedimage) createimage(imgwidth, imgheight); graphics2d g2 = bi.creategraphics(); g2.drawimage(img, null, null); fileoutputstream out = ...

    NyARToolkit:从 NyARToolkit-4.1.1 (http

    将网络摄像头捕获库( )与 NyARToolkit java3d utils(而不是 JMF)集成。 使用 webcam-capture 库的类包含在以下项目中。 NyARToolkit.utils.java3d.webcam-capture NyARToolkit.utils.webcam-capture ...

    Panic Player-开源

    使用 JMF 的操作系统独立音频播放器。 Panic Player 应该支持多种输入和输出格式。 可以使用其他示例文件来操作输入流。

    DistributedOptimizationWithLocalDomains:在纸上复制实验的代码

    JFC Mota,JMF Xavier,PMQ Aguiar,M.Püschel。 IEEE Transactions on Automatic Control,第1卷。 60,No.7,pp.2004-2009,2015。 , 主要算法是DADMM_Partial.m 。 所有代码都在Matlab中实现。 尽管所有算法都...

    jsr80 java 访问 usb

    该 API 支持控制传输、批量传输和中断传输,不支持等时传输,因为等时传输用于媒体数据(如音频和视频),JMF API 已经在其他标准设备驱动程序上对此提供了很好的支持(参阅 参考资料)。当前,该 API 可以在具有 Linux ...

Global site tag (gtag.js) - Google Analytics