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 
" + "<html><h3> :
" + "<html><h3>Email:[email protected]", " ",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