学校毕业设计的题目是基于rtsp协议的流媒体客户端的实现,通过查找资料,发现Java媒体框架(JMF)使你能够编写出功能强大的多媒体程序,却不用关心底层复杂的实现细节。因此,就试着接触了一下,发现很容易上手,下面是一个简单的媒体播放器:public class VideoPlayer1 extends JFrame implements ControllerListener{
private int videoWidth=0;
private int videoHeight=0;
private int controlHeight=30;
private int insetWidth=10;
private int insetHeight=30;
private Player player;
private Player player1;
private Player player2;
private Player player3;
private Component visual; //播放组件
private Component visual1;
private Component visual2; //播放组件
private Component visual3;
private Component control=null;//控制组件
private Component control1=null;//控制组件
private Component control2=null;//控制组件
private Component control3=null;//控制组件
private JPanel panel;
private JPanel panel1;
private JPanel panel2;
private JPanel panel3;
private GridLayout gridLayout;
public VideoPlayer1(){
super("视频播放器");
gridLayout=new GridLayout(2,2);
this.setLayout(gridLayout);
Container contentPanel=this.getContentPane();
panel=new JPanel();
panel.setLayout(new BorderLayout());
panel1=new JPanel();
panel1.setLayout(new BorderLayout());
panel2=new JPanel();
panel2.setLayout(new BorderLayout());
panel3=new JPanel();
panel3.setLayout(new BorderLayout());
contentPanel.add(panel);
contentPanel.add(panel1);
contentPanel.add(panel2);
contentPanel.add(panel3);
setSize(1000,800);
setVisible(true);
this.play();
this.addListener();
}
public void addListener(){
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent arg0) {
// TODO Auto-generated method stub
//System.exit(0);
player.stop();
player1.stop();
player2.stop();
player3.stop();
VideoPlayer1.this.dispose();
}
});
}
public void play(){
URL url=null;
URL url1=null;
File file=null;
File file1=null;
try {
//准备一个要播放的视频文件的URL
file=new File("F:\\青春的痕迹-2012-4-21第二版.mpg");
url=file.toURI().toURL();
file1=new File("F:\\10 雪花企业介绍视频短片.mpg");
url1=file1.toURI().toURL();
//通过调用Manager的createPlayer方法来创建一个Player对象
//这个对象时媒体播放的核心控制对象
player=Manager.createPlayer(url1);
player1=Manager.createPlayer(url1);
player2=Manager.createPlayer(url1);
player3=Manager.createPlayer(url1);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoPlayerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//对Player对象注册监听器,能在相关事件发生的时候执行相关的动作
player.addControllerListener(this);
player.realize();
player1.addControllerListener(this);
player1.realize();
player2.addControllerListener(this);
player2.realize();
player3.addControllerListener(this);
player3.realize();
//让Player对象进行相关资源的分配
}
public static void main(String[] args) {
// TODO Auto-generated method stub
VideoPlayer1 vp=new VideoPlayer1();
vp.play();
}
@Override
public void controllerUpdate(ControllerEvent ce) {
// TODO Auto-generated method stub
if(ce instanceof RealizeCompleteEvent){
//player实例化完成后进行Player播放前预处理
player.prefetch();
}else if(ce instanceof PrefetchCompleteEvent){
if(visual!=null)
return ;
//获得Player中播放视频的组件,���得到视频窗口的大小
//然后把视频窗口的组件添加到Frame窗口中,
if((visual=player.getVisualComponent())!=null){
Dimension size=visual.getPreferredSize();
videoWidth=400;
videoHeight=350;
panel.add(visual);
}else{
videoWidth=320;
}
//取得Player中的视频播放控制组组件,并把该组件添加到Frame
//窗口中
if((this.control=player.getControlPanelComponent())!=null){
this.controlHeight=control.getPreferredSize().height;
panel.add(control,BorderLayout.SOUTH);
}
//设定Frame窗口的大小,使得满足视频文件的默认大小
//setSize(this.videoWidth+this.insetWidth,
//this.videoHeight+this.controlHeight);
validate();
//启动视频播放组件开始播放
player.start();
}else if(ce instanceof EndOfMediaEvent){
//当播放视频完成后,把时间进度条回复到开始,并再次
//重新开始播放
player.setMediaTime(new Time(0));
player.start();
}
if(ce instanceof RealizeCompleteEvent){
//player实例化完成后进行Player播放前预处理
player1.prefetch();
}else if(ce instanceof PrefetchCompleteEvent){
if(visual1!=null)
return ;
//获得Player中播放视频的组件,并得到视频窗口的大小
//然后把视频窗口的组件添加到Frame窗口中,
if((visual1=player1.getVisualComponent())!=null){
Dimension size=visual1.getPreferredSize();
videoWidth=400;
videoHeight=350;
panel1.add(visual1);
}else{
videoWidth=320;
}
//取得Player中的视频播放控制组组件,并把该组件添加到Frame
//窗口中
if((this.control1=player1.getControlPanelComponent())!=null){
this.controlHeight=control1.getPreferredSize().height;
panel1.add(control1,BorderLayout.SOUTH);
}
//设定Frame窗口的大小,使得满足视频文件的默认大小
//setSize(this.videoWidth+this.insetWidth,
//this.videoHeight+this.controlHeight);
validate();
//启动视频播放组件开始播放
player1.start();
}else if(ce instanceof EndOfMediaEvent){
//当播放视频完成后,把时间进度条回复到开始,并再次
//重新开始播放
player1.setMediaTime(new Time(0));
player1.start();
}
if(ce instanceof RealizeCompleteEvent){
//player实例化完成后进行Player播放前预处理
player2.prefetch();
}else if(ce instanceof PrefetchCompleteEvent){
if(visual2!=null)
return ;
//获得Player中播放视频的组件,并得到视频窗口的大小
//然后把视频窗口的组件添加到Frame窗口中,
if((visual2=player2.getVisualComponent())!=null){
Dimension size=visual2.getPreferredSize();
videoWidth=400;
videoHeight=350;
panel2.add(visual2);
}else{
videoWidth=320;
}
//取得Player中的视频播放控制组组件,并把该组件添加到Frame
//窗口中
if((this.control2=player2.getControlPanelComponent())!=null){
this.controlHeight=control2.getPreferredSize().height;
panel2.add(control2,BorderLayout.SOUTH);
}
//设定Frame窗口的大小,使得满足视频文件的默认大小
//setSize(this.videoWidth+this.insetWidth,
//this.videoHeight+this.controlHeight);
validate();
//启动视频播放组件开始播放
player2.start();
}else if(ce instanceof EndOfMediaEvent){
//当播放视频完成后,把时间进度条回复到开始,并再次
//重新开始播放
player2.setMediaTime(new Time(0));
player2.start();
}
if(ce instanceof RealizeCompleteEvent){
//player实例化���成后进行Player播放前预处理
player3.prefetch();
}else if(ce instanceof PrefetchCompleteEvent){
if(visual3!=null)
return ;
//获得Player中播放视频的组件,并得到视频窗口的大小
//然后把视频窗口的组件添加到Frame窗口中,
if((visual3=player3.getVisualComponent())!=null){
Dimension size=visual3.getPreferredSize();
videoWidth=400;
videoHeight=350;
panel3.add(visual3);
}else{
videoWidth=320;
}
//取得Player中的视频播放控制组组件,并把该组件添加到Frame
//窗口中
if((this.control3=player3.getControlPanelComponent())!=null){
this.controlHeight=control3.getPreferredSize().height;
panel3.add(control3,BorderLayout.SOUTH);
}
//设定Frame窗口的大小,使得满足视频文件的默认大小
//setSize(this.videoWidth+this.insetWidth,
//this.videoHeight+this.controlHeight);
validate();
//启动视频播放组件开始播放
player3.start();
}else if(ce instanceof EndOfMediaEvent){
//当播放视频完成后,把时间进度条回复到开始,并再次
//重新开始播放
player3.setMediaTime(new Time(0));
player3.start();
}
}
}
效果图:
如果,感兴趣的话,可以通过重写JFrame,美化界面,还可以添加一个FileDialog来实现选择视频!