import javax.media.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MediaPlay extends Frame implements ActionListener,
ControllerListener,ItemListener{//实现一个播放器程序
Player palyer;
Component vedio_Component;//视频组件
Component control_Component;//控制组件
boolean first =true;
boolean loop_flag=false;
String currentDirectory;//文件当前目录
FileDialog fileDialog;
public MediaPlay(String title){//构造方法
super(title);
addWindowListener(new WindowAdapter(){//关闭jmf
public void widowClosing(WindowEvent e){
dispose();
}
public void windowClosed(WindowEvent e){
if(player!=null){
player.close();
}
System.exit(0);
}
});
Menu menu=new Menu("文件");//构造播放器的菜单
MenuItem openItem=new MenuItem("文件选择");
openItem.addActionListener(this);
menu.add(openItem);
openItem.setActionCommand("open");
menu.addSeparator();
CheckboxMenuItem loopItem=new CheckboxMenuItem("循环播放");
loopItem.addActionListener(this);
loopItem.setActionCommand("loop");
menu.add(loopItem);
menu.addSeparator();
MenuItem exitItem=new MenuItem("退出");
exitItem.addActionListener(this);
exitItem.setActionCommand("exit");
menu.add(exitItem);
MenuBar menuBar=new MenuBar();
menuBar.add(menu);
this.setMenuBar(menuBar);
this.setSize(300,250);
this.setVisible(true);
}
//实现了ActionListener接口,处理组件的活动事件
public void actionPerformed(ActionEvent e){
this.setSize(300,250);
if(e.getActionCommand().equals("exit")){
dispose();//调用dispose()
}else{
fileDialog=new FileDialog(this,"打开媒体文件",FileDialog.LOAD);
fileDialog.setDirectory(currentDirectory);
fileDialog.setVisible(true);
if(fileDialog.getFile()==null){//如果用户放弃选择文件就返回
return;
}
currentDirectory=fileDialog.getDirectory();
if(player!=null){
player.close();//关闭已经存在的jmf播放对象
}
try{//创建一个打开文件的播放器
player=Manger.createPlayer(new MediaLocator("file:" +
fileDialog.getDirectory()+fileDialog.getFile()));
}catch(java.io.IOException e1){
System.out.println("播放文件失败");
return;
}catch(NoPlayerException e1){
System.out.println("无法找到播放文件");
return;
}
if(player==null){
System.out.println("无法创建播放器");
return;
}
first =false;
this.setTitle(fileDialog.getFile());
player.addControllerListener(this);//播放器控制事件的处理
player.prefetch();
}
}
//实现ControllerListener接口,处理播放器控制事件
public void controllerUpdate(ControllerEvent e){
this.setSize(300,250);
//
if(e instanceof ControllerClosedEvent){
if(vedio_Component!=null){
remove(vedio_Component);
this.vedio_Component=null;
}
if(control_Component!=null){
remove(control_Component);
this.vedio_Component=null;
}
return;
}
if(e instanceof EndOfMeidaEvent){//如果是媒体文件就到达尾部事件
if(loop_flag){
player.setMeidaTime(new Time(0));//如果允许循环就重复播放
player.start();
}
return;
}
if(e instanceof PrefetchCompleteEvent){//如果是预读事件
player.start();
return;
}
if(e instanceof RealizeCompleteEvent){//如果文件打开完全事件,就显示视屏组件和控制组件
vedio_Component=player.getVisualComponent();
if(vedio_Component!=null){
add(vedio_Component);
}
control_Component=player.getControlPaneComponent();
if(control_Component!=null){
add(control_Component,BorderLayout.SOUTH);
}
this.pack();
}
}
//处理循环复选框菜单项的单击事件
public void itemStateChanged(ItemEvent e){
loop_flag=!loop_flag;
}
public void paint(Graphics g){
int w=getSize().width;
int h=getSize().height;
g.setColor(Color.black);
g.fillRect(0,0,w,h);
if(first){
Font f=new Font("DialogInput",Font.BOLD,14);
g.setFont(f);
FontMetrics fm=g.getFontMetrics();
int swidth=fm.stringWidth("***欢迎使用JMF多媒体播放器***");
g.setColor(Color.yellow);
g.drawString("***欢迎使用JMF多媒体播放器***",
(w-swidth)/2,(h+getInsets().top/2));
}else{
String s=fileDialog.getFile();
Font f=new Font("DialogInput",Font.BOLD,16);
g.setFont(f);
FontMetrics fm=g.getFontMetrics();
int swidth=fm.stringWidth("您心在欣赏的曲目是:"+s);
g.setColor(Color.pink);
g.drawString("您现在欣赏的曲目是:"+s,
(w-swidth)/2,(h+getInsets().top/2));
}
//调用超类的paint()方法
super.paint(g);
}
public void update(Graphics g){
paint(g);
}
public static void main(String[] args){
new MediaPlay("JMF多媒体播放器" +
"");
}
}
程序需要的jmf包也已经导入,但是总是提示我找不到Player类,在eclipse中无法运行。希望大神指点
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MediaPlay extends Frame implements ActionListener,
ControllerListener,ItemListener{//实现一个播放器程序
Player palyer;
Component vedio_Component;//视频组件
Component control_Component;//控制组件
boolean first =true;
boolean loop_flag=false;
String currentDirectory;//文件当前目录
FileDialog fileDialog;
public MediaPlay(String title){//构造方法
super(title);
addWindowListener(new WindowAdapter(){//关闭jmf
public void widowClosing(WindowEvent e){
dispose();
}
public void windowClosed(WindowEvent e){
if(player!=null){
player.close();
}
System.exit(0);
}
});
Menu menu=new Menu("文件");//构造播放器的菜单
MenuItem openItem=new MenuItem("文件选择");
openItem.addActionListener(this);
menu.add(openItem);
openItem.setActionCommand("open");
menu.addSeparator();
CheckboxMenuItem loopItem=new CheckboxMenuItem("循环播放");
loopItem.addActionListener(this);
loopItem.setActionCommand("loop");
menu.add(loopItem);
menu.addSeparator();
MenuItem exitItem=new MenuItem("退出");
exitItem.addActionListener(this);
exitItem.setActionCommand("exit");
menu.add(exitItem);
MenuBar menuBar=new MenuBar();
menuBar.add(menu);
this.setMenuBar(menuBar);
this.setSize(300,250);
this.setVisible(true);
}
//实现了ActionListener接口,处理组件的活动事件
public void actionPerformed(ActionEvent e){
this.setSize(300,250);
if(e.getActionCommand().equals("exit")){
dispose();//调用dispose()
}else{
fileDialog=new FileDialog(this,"打开媒体文件",FileDialog.LOAD);
fileDialog.setDirectory(currentDirectory);
fileDialog.setVisible(true);
if(fileDialog.getFile()==null){//如果用户放弃选择文件就返回
return;
}
currentDirectory=fileDialog.getDirectory();
if(player!=null){
player.close();//关闭已经存在的jmf播放对象
}
try{//创建一个打开文件的播放器
player=Manger.createPlayer(new MediaLocator("file:" +
fileDialog.getDirectory()+fileDialog.getFile()));
}catch(java.io.IOException e1){
System.out.println("播放文件失败");
return;
}catch(NoPlayerException e1){
System.out.println("无法找到播放文件");
return;
}
if(player==null){
System.out.println("无法创建播放器");
return;
}
first =false;
this.setTitle(fileDialog.getFile());
player.addControllerListener(this);//播放器控制事件的处理
player.prefetch();
}
}
//实现ControllerListener接口,处理播放器控制事件
public void controllerUpdate(ControllerEvent e){
this.setSize(300,250);
//
if(e instanceof ControllerClosedEvent){
if(vedio_Component!=null){
remove(vedio_Component);
this.vedio_Component=null;
}
if(control_Component!=null){
remove(control_Component);
this.vedio_Component=null;
}
return;
}
if(e instanceof EndOfMeidaEvent){//如果是媒体文件就到达尾部事件
if(loop_flag){
player.setMeidaTime(new Time(0));//如果允许循环就重复播放
player.start();
}
return;
}
if(e instanceof PrefetchCompleteEvent){//如果是预读事件
player.start();
return;
}
if(e instanceof RealizeCompleteEvent){//如果文件打开完全事件,就显示视屏组件和控制组件
vedio_Component=player.getVisualComponent();
if(vedio_Component!=null){
add(vedio_Component);
}
control_Component=player.getControlPaneComponent();
if(control_Component!=null){
add(control_Component,BorderLayout.SOUTH);
}
this.pack();
}
}
//处理循环复选框菜单项的单击事件
public void itemStateChanged(ItemEvent e){
loop_flag=!loop_flag;
}
public void paint(Graphics g){
int w=getSize().width;
int h=getSize().height;
g.setColor(Color.black);
g.fillRect(0,0,w,h);
if(first){
Font f=new Font("DialogInput",Font.BOLD,14);
g.setFont(f);
FontMetrics fm=g.getFontMetrics();
int swidth=fm.stringWidth("***欢迎使用JMF多媒体播放器***");
g.setColor(Color.yellow);
g.drawString("***欢迎使用JMF多媒体播放器***",
(w-swidth)/2,(h+getInsets().top/2));
}else{
String s=fileDialog.getFile();
Font f=new Font("DialogInput",Font.BOLD,16);
g.setFont(f);
FontMetrics fm=g.getFontMetrics();
int swidth=fm.stringWidth("您心在欣赏的曲目是:"+s);
g.setColor(Color.pink);
g.drawString("您现在欣赏的曲目是:"+s,
(w-swidth)/2,(h+getInsets().top/2));
}
//调用超类的paint()方法
super.paint(g);
}
public void update(Graphics g){
paint(g);
}
public static void main(String[] args){
new MediaPlay("JMF多媒体播放器" +
"");
}
}
程序需要的jmf包也已经导入,但是总是提示我找不到Player类,在eclipse中无法运行。希望大神指点
我把楼主代码复制到本地,把拼写错误改了,引入了jmf包,程序是可以运行的。除了楼主import的包之外并没有import其它包
change references in other files)