使用JMF实现一个媒体播放器,它可以是一个多媒体文件(mpeg、MP3格式)

package player;
import javax.media.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class MediaPlay extends Frame implements ActionListener, ControllerListener, ItemListener{
    Player player;   //JMF播放器
    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(){
    public void windowClosing(WindowEvent e){  //用户单机窗口系统菜单的关闭按钮
    dispose();  //调用dispose以执行windowClosed
    }
    public void windowClosed(WindowEvent e){
    if(player != null){
    player.close();  //关闭JMF播放器
    }
    System.exit(0);
    }  //创建播放器的菜单
    });
    Menu menu= new Menu("文件");
    MenuItem open_Item = new MenuItem("文件选择") ;
    open_Item.addActionListener(this);
    menu.add(open_Item);
    open_Item.setActionCommand("open");
    menu.addSeparator();
    CheckboxMenuItem loop_Item = new CheckboxMenuItem("循环播放");
   
    loop_Item.addItemListener(this);
    loop_Item.setActionCommand("loop");
    menu.add(loop_Item);
    menu.addSeparator();
    MenuItem exit_Item = new MenuItem("推出播放器");
    exit_Item.addActionListener(this);
    exit_Item.setActionCommand("exit");
    menu.add(exit_Item);
    MenuBar menuBar = new MenuBar();
    menuBar.add(menu);
    this.setMenuBar(menuBar);
    this.setSize(300, 250);
    this.setVisible(true);
    }
    public void actionPerformed(ActionEvent e){
    this.setSize(300, 250);
    if(e.getActionCommand().equals("exit")){
    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();
    }
    try{
    player = Manager.createPlayer(new MediaLocator("file:" + fileDialog.getDirectory() + fileDialog.getFile()));
    }catch(java.io.IOException e2){
    System.out.println("播放文件错误");
    return;
    }catch(NoPlayerException e2){
    System.out.println( "不能找到播放器");
    return;
    }
    if(player == null){
    System.out.println("无法创建播放器");
    return;
    }
    first = false;
    this.setTitle(fileDialog.getFile());
    player.addControllerListener(this);
    player.prefetch();
    }
    }
    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.control_Component = null;
    }
    return;
    }
    if(e instanceof EndOfMediaEvent){
    if(loop_flag){
   
    player.setMediaTime(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.getControlPanelComponent();
    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多媒体播放器2 ***");
    g.setColor(Color.yellow);
    g.drawString("*** 欢迎使用JMF多媒体播放器2 ***", (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);
    }
    super.paint(g);
   }
    public void updata(Graphics g){
    paint(g);
    }
    public static void main(String[] args)
    {
    new MediaPlay("JMF 多媒体播放器");
    }
}

你可能感兴趣的:(swing,F#)