JMF示例(二)

本文介绍如何在Java中将Swing组件与Java Media Framework (JMF)进行整合,实现多媒体文件的播放功能。通过设置Manager.HINT来启用轻量级组件渲染,并展示了具体的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<!--<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来使其只能使用轻量级绘制器,此后创建的任何播放器都将使用轻量级绘制器。注意的是存在父容器中的组件的双缓存功能应该要开启。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值