JMF播放器小例子

/*

jdk 1.6 ,需要配置环境变量

JMF 2.1.1,先安装,需要配置环境变量。

例如:
PATH 在末尾添加
C:\Program Files\Java\jdk1.6.0_10\bin;

CLASSPATH
.;C:\JMF211\lib\sound.jar;C:\JMF211\lib\jmf.jar;C:\JMF211\lib;

*/

import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.media.*;

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Vector;

public class MediaPlayerFrame extends JFrame implements ActionListener, ControllerListener{
public static final int WIDTH=880;
public static final int HEIGHT=660;
private ImageIcon leftArrowIcon=new ImageIcon("image/left_arrow.gif");
    private ImageIcon rightArrowIcon=new ImageIcon("image/right_arrow.gif");
private static final long serialVersionUID = 1L;
private JFileChooser chooseFileChooser=new JFileChooser("res/");
private Component videoPanel;
private Component contollerPanel;
private JPanel mainPanel;
private JPanel rightPanel;
private JPanel playListPanel;
private JButton controlPlayListPanelButton;
private JPanel buttonPanel;
private JButton addFileButton;
    private JButton removeOneListItemButton;
    private JScrollPane scrollPane;

private boolean hasPlayList=true;
private JList playList=null;
private DefaultListModel playListModel;
private Vector<String> playListVector=new Vector<String>();
private Hashtable<String,String> playListHashtable=new Hashtable<String,String>();
//private int lastSelectedIndex=-1;
private Player player = null;
private boolean isTheFirstTimeToPlayMedia=true;


public static void main(String[] args) {
   try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
   } catch(Exception e) {
        e.printStackTrace();
   }

   MediaPlayerFrame frame=new MediaPlayerFrame();
   frame.setTitle("JAVA媒体播放器");
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   frame.setSize(WIDTH, HEIGHT);
   //frame.pack();
     //Center the window
     Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
     Dimension frameSize = frame.getSize();
     frame.setLocation((screenSize.width - frameSize.width) / 2,
        (screenSize.height - frameSize.height) / 2);
     //show the window
   frame.setVisible(true);
   //frame.setExtendedState(JFrame.MAXIMIZED_BOTH); //完全最大化
}

public MediaPlayerFrame() {
   super();

   //create   the   menu
        MenuBar   mb   =   new   MenuBar();
        this.setMenuBar(mb);

        Menu   fileMenu   =   new   Menu("文件");
        mb.add(fileMenu);
        MenuItem   itemPlay   =   new   MenuItem("打开&播放");
        itemPlay.addActionListener(this);
        fileMenu.add(itemPlay);
        MenuItem   itemStop   =   new   MenuItem("停止");
        itemStop.addActionListener(this);
        fileMenu.add(itemStop);
        MenuItem   itemExit   =   new   MenuItem("退出");
        itemExit.addActionListener(this);
        fileMenu.add(itemExit);

        Menu helpMenu=new Menu("帮助");
        mb.add(helpMenu);
        MenuItem aboutMenuItem=new MenuItem("关于") ;
        helpMenu.add(aboutMenuItem);
        aboutMenuItem.addActionListener(this);

        mainPanel = new JPanel();
        mainPanel.setLayout(new BorderLayout());
        this.getContentPane().add(mainPanel,BorderLayout.CENTER); //添加播放组件

        rightPanel=new JPanel();
        rightPanel.setLayout(new BorderLayout());
        controlPlayListPanelButton=new JButton(rightArrowIcon); //初始化时有播放列表
        controlPlayListPanelButton.addActionListener(this);

        playListPanel=new JPanel();
        playListPanel.setLayout(new BorderLayout());

        buttonPanel=new JPanel();
        addFileButton=new JButton("添加");
        removeOneListItemButton=new JButton("删除");
        addFileButton.addActionListener(this);
        removeOneListItemButton.addActionListener(this);
        buttonPanel.add(addFileButton);
        buttonPanel.add(removeOneListItemButton);

        playList = new JList();
        playListModel = new DefaultListModel();
        playList.setModel(playListModel);
        playList.addMouseListener(new MouseAdapter() {
    public void mouseClicked(MouseEvent e) {
     listAction(e);
    }
   });
   scrollPane = new JScrollPane(playList);
   scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

   playListPanel.add(buttonPanel,BorderLayout.NORTH);
   playListPanel.add(scrollPane,BorderLayout.CENTER);


   rightPanel.add(controlPlayListPanelButton,BorderLayout.WEST); //添加显示/隐藏小按钮
   rightPanel.add(playListPanel,BorderLayout.CENTER); //添加播放列表组件

        this.getContentPane().add(rightPanel,BorderLayout.EAST);

        hidePlayListPanel();

    }

public void actionPerformed(ActionEvent e) {
   String arg=e.getActionCommand();

        if(arg.equals("打开&播放")) {
            play();
        }

        if(arg.equals("停止")) {
        stop();
        }

        if(arg.equals("退出")) {
        stop();
            this.dispose();
            System.exit(0);
        }

        if(arg.equals("添加")) {
        //利用文件选择对话框添加一个音乐文件到播放列表末尾
        chooseFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    int returnVal =chooseFileChooser.showOpenDialog(null);
    if(returnVal==JFileChooser.APPROVE_OPTION) {
     File oneFile=chooseFileChooser.getSelectedFile();
     String theFileName=oneFile.getName();
     String theFilePath=oneFile.getPath();
     this.playListVector.add(theFileName);
     this.playListHashtable.put(theFileName,theFilePath);
     updatePlayList();
    }

        }

        if(arg.equals("删除")) {
        //删除所选择的列表项
        int lastSelectedIndex=playList.getSelectedIndex();
        String selectedItemValue=(String)playList.getSelectedValue();
        if(lastSelectedIndex<0){
           return;
        }else{
           playListModel.removeElementAt(lastSelectedIndex);
           this.playListVector.remove(lastSelectedIndex);
           this.playListHashtable.remove(selectedItemValue);
           updatePlayList();
        }


        }

        if(e.getSource()==controlPlayListPanelButton){
        if(hasPlayList){
           controlPlayListPanelButton.setIcon(leftArrowIcon);
           hidePlayListPanel();
           hasPlayList=false;
        }else{
           controlPlayListPanelButton.setIcon(rightArrowIcon);
           showPlayListPanel();
           hasPlayList=true;
        }
        SwingUtilities.updateComponentTreeUI(this.getContentPane());
        }

   if(arg.equals("关于")) {
    JOptionPane.showMessageDialog(this, "<html><h3>JAVA媒体播放器 V1.0 \n" +
      "<html><h3>作者:施小平\n" +
      "<html><h3>Email:newton111@126.com",
      "关于",JOptionPane.INFORMATION_MESSAGE);
   }

}

public synchronized void controllerUpdate(ControllerEvent event) {
   if(event instanceof RealizeCompleteEvent) {
            showController();
        }

}


private void play() {
   stop();
   String filePath=null;
   String fileName=null;
   try{
    chooseFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    int returnVal =chooseFileChooser.showOpenDialog(null);
    if(returnVal==JFileChooser.APPROVE_OPTION) {
     File oneFile=chooseFileChooser.getSelectedFile();
     fileName=oneFile.getName();
     filePath=oneFile.getPath();
     //create the player
             setPlayer(filePath);
             //add a listener for player state changes
             player.addControllerListener(this);
             player.start();
             //player.realize();
             showController();

             this.playListVector.add(fileName);
     this.playListHashtable.put(fileName,filePath);
     updatePlayList();

             this.setTitle("JAVA媒体播放器----"+fileName);
             System.out.println("正在播放"+fileName);

             if(isTheFirstTimeToPlayMedia){
      showPlayListPanel();
      isTheFirstTimeToPlayMedia=false;
     }
    }
        }catch(Exception   e) {
        player=null;
            e.printStackTrace();
        }
}


private void stop() {
   if(player!=null){
    player.stop();
    player.close();
    mainPanel.removeAll();
   }
}

private void setPlayer(String filePath) throws
    Exception,NoPlayerException, CannotRealizeException{
   this.player = Manager.createRealizedPlayer(new MediaLocator(""+ filePath));
}

private void showController() {
   videoPanel=null;
   contollerPanel=null;

   if((videoPanel=player.getVisualComponent())!=null) {
    mainPanel.add(videoPanel,BorderLayout.CENTER);
   }else{
      System.out.println("Unable to get visual component");
   }

   if((contollerPanel=player.getControlPanelComponent())!=null) {
    mainPanel.add(contollerPanel,BorderLayout.SOUTH);
   }else {
    System.out.println("Unable to get ControlPanelComponent");
   }

   SwingUtilities.updateComponentTreeUI(mainPanel);

}

private void listAction(MouseEvent e){
   //int lastSelectedIndex=playList.getSelectedIndex();
   String selectedItemValue=(String)playList.getSelectedValue();
   if (e.getClickCount() == 2) {
    String filePath=playListHashtable.get(selectedItemValue);
    if(filePath!=null){
     stop();
     try {
      setPlayer(filePath);
     } catch (Exception e1) {
      e1.printStackTrace();
     }
             player.addControllerListener(this);
             player.start();
             showController();
             this.setTitle("JAVA媒体播放器----"+selectedItemValue);
             System.out.println("正在播放"+selectedItemValue);
    }
   }
}


//点击添加、删除按钮、菜单打开文件时更新播放列表
private void updatePlayList(){
   playListModel.removeAllElements();
   for(int i=0;i<playListVector.size();i++){
    String tempStr=null;
    tempStr=playListVector.get(i);
    playListModel.addElement(tempStr);
   }
}


private void showPlayListPanel(){
   playListPanel.add(buttonPanel,BorderLayout.NORTH);
   playListPanel.add(scrollPane,BorderLayout.CENTER);
   SwingUtilities.updateComponentTreeUI(rightPanel);
}


private void hidePlayListPanel(){
   playListPanel.removeAll();
   SwingUtilities.updateComponentTreeUI(playListPanel);
}

static
    { //重新设置默认字体
         Font defaultFont=new Font("宋体",Font.PLAIN,12);
         UIManager.put("Button.font",defaultFont);
         UIManager.put("ToggleButton.font",defaultFont);
         UIManager.put("Label.font",defaultFont);
         UIManager.put("CheckBox.font",defaultFont);
         UIManager.put("RadioButton.font",defaultFont);
         UIManager.put("TabbedPane.font",defaultFont);
         UIManager.put("TitledBorder.font",defaultFont);
         UIManager.put("Table.font",defaultFont);
         UIManager.put("List.font",defaultFont);
         UIManager.put("TableHeader.font",defaultFont);
         UIManager.put("ToolTip.font",defaultFont);
         UIManager.put("ComboBox.font",defaultFont);
         UIManager.put("Menu.font",defaultFont);
         UIManager.put("MenuItem.font",defaultFont);
         UIManager.put("CheckBoxMenuItem.font",defaultFont);
         UIManager.put("RadioButtonMenuItem.font",defaultFont);
    }


}
转载地址: http://apps.hi.baidu.com/share/detail/46158807
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值