Chris Pollett >
Students > [Bio] [Del1] [Del3] |
A Simple Media PlayerDescription: This simple java program is used to experiment with the functionalities of the Java Media Framework(JMF) API. This API provides ability of adding audio and video to Java applications and applets. JMF has built-in features to integrate media with Java Swing Components. In this program, the class Manager is used to create an instance of the Player interface wich is created and used only for the given media file. Example: Media Player AppletUsage: choose a media file from the file list, open it, then press the play button on the control bar. Notice: there is a bug in this application; you need to double click the "open" button at the first time when you try to play a file. /** An Java Applet which is able to play JMF recognizable media @author Xunyan Yang @version 1.0 2004/02/29 */ import javax.media.*; //import media package defined by JMF import javax.swing.*; import java.awt.*; import java.awt.event.*; import javax.swing.event.*; import java.io.File; import java.io.IOException; import java.net.URL; import java.net.MalformedURLException; public class MediaPlayerApplet extends JApplet { //set up volum slider's parameters static final int VOLUM_MIN = 0; static final int VOLUM_MAX = 10; static final int VOLUM_INIT = 2; static final float VOLUM_INIT_LEVEL = 0.2f; URL codeBase; // Support format private String formatList = "mov avi"; private JList fileList; private JButton openButton = new JButton("Open"); private Player player = null; private Component comp; private Time currentPlayTime = new Time(0); public void init() { // read file list into left side JList URL workingURL = getURL("medialibrary"); File workingDir = new File(workingURL.getFile()); if (workingDir.exists() && workingDir.isDirectory()) { DefaultListModel listModel = new DefaultListModel(); File[] files = workingDir.listFiles(); for (int i = 0; i < files.length; i++) { File file = files[i]; if (formatList.indexOf(getFileType(file.getName())) != -1) { listModel.addElement(file.getName()); } } fileList = new JList(listModel); } else fileList = new JList(); //left side panel JPanel fileChooserPane = new JPanel(); fileChooserPane.setLayout(new BorderLayout()); fileChooserPane.setPreferredSize(new Dimension(100, 300)); fileChooserPane.add(new JScrollPane(fileList), BorderLayout.CENTER); fileChooserPane.add(openButton, BorderLayout.SOUTH); Container con = this.getContentPane(); con.setLayout(new BorderLayout()); con.add(fileChooserPane, BorderLayout.WEST); openButton.addActionListener(new OpenAction()); } //get the type of the file. private String getFileType(String fileName) { int pos = fileName.indexOf("."); return (fileName.substring(pos + 1, fileName.length())); } //get the absolute path of the current folder protected URL getURL(String filename) { URL url = null; if (codeBase == null) { codeBase = getCodeBase(); } try { url = new URL(codeBase, filename); } catch (java.net.MalformedURLException e) { e.printStackTrace(); return null; } return url; } //create a new Player instance for a given media file private void playFile(File mediaFile) { try { setMediaLocator(new MediaLocator(mediaFile.toURL())); } catch (Exception e) { e.printStackTrace(); } } public void setMediaLocator(MediaLocator locator) throws IOException, NoPlayerException, CannotRealizeException { setPlayer(Manager.createRealizedPlayer(locator)); } public void setPlayer(Player newPlayer) { // close the current player if (player != null) closeCurrentPlayer(); player = newPlayer; if (player == null) return; this.getContentPane().add(createRightPanel(), BorderLayout.CENTER); } /** generate an instance of JPanel(the main control panel) containing visual, visual controling and volume controling components */ public JPanel createRightPanel() { JPanel rightPanel = new JPanel(); rightPanel.setLayout(new BorderLayout()); //A slider used to control the volume JSlider volumnSlider = new JSlider(JSlider.HORIZONTAL, VOLUM_MIN, VOLUM_MAX, VOLUM_INIT); volumnSlider.setToolTipText("Volumn Control"); //the component slider adds the instance of VolumnController as a state change listener, //so when the state of the slider is changed, this instance will be notified. volumnSlider.addChangeListener(new VolumnController()); volumnSlider.setSize(30, volumnSlider.getHeight()); //get GainControl GainControl volumControl = player.getGainControl(); //set the initial volume volumControl.setLevel(VOLUM_INIT_LEVEL); if ((comp = player.getVisualComponent()) != null) { rightPanel.add(comp, BorderLayout.CENTER); } if ((comp = player.getControlPanelComponent()) != null) { rightPanel.add(comp, BorderLayout.NORTH); } JPanel southPanel = new JPanel(); southPanel.setLayout(new BorderLayout()); JLabel label = new JLabel("Volume"); southPanel.add(label, BorderLayout.WEST); southPanel.add(volumnSlider, BorderLayout.CENTER ); rightPanel.add(southPanel,BorderLayout.SOUTH); validate(); return rightPanel; } //Stops and closes the current player if a player already exists. private void closeCurrentPlayer() { if (player != null) { player.stop(); player.deallocate(); } rightPanel.removeAll(); comp = null; } // Volumn Controller class VolumnController implements ChangeListener { public void stateChanged(ChangeEvent e) { //get GainControl GainControl volumControl = player.getGainControl(); JSlider source = (JSlider) e.getSource(); //if the knob of the volume slider is moved if (source.getValueIsAdjusting()) { //convert volume scale to float form (0.0 - 1.0) float newVolum = ((float) source.getValue()) / 10; volumControl.setLevel(newVolum); } } } //Open a new file class OpenAction implements ActionListener { public void actionPerformed(ActionEvent e) { String mediaFileName = fileList.getSelectedValue().toString(); if (!mediaFileName.equals("")) { URL workingURL = getURL("medialibrary"); File mediaFile = new File(workingURL.getFile() + File.separator + mediaFileName); closeCurrentPlayer(); playFile(mediaFile); } } } } |