<!--<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();
}
}
}
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组件使用JMF。Swing组件是轻量级组件,而JMF默认使用的是重量级组件(它们可以为高速率的视频使用本地的绘制方法)。
解决方案:
JMF2.0包含了几个不同的视频绘制器,可以强制让播放器使用轻量级组件。示例如下:
Manager.setHint(Manager.LIGHTWEIGHT_RENDERER,
new Boolean(true));
这在内部是通过
PlugInManager
来使其只能使用轻量级绘制器,此后创建的任何播放器都将使用轻量级绘制器。注意的是存在父容器中的组件的双缓存功能应该要开启。