1 // Fig. 21.6: MediaPanel.java 2 // A JPanel the plays media from a URL 3 import java.awt.BorderLayout; 4 import java.awt.Component; 5 import java.io.IOException; 6 import java.net.URL; 7 import javax.media.CannotRealizeException; 8 import javax.media.Manager; 9 import javax.media.NoPlayerException; 10 import javax.media.Player; 11 import javax.swing.JPanel; 12 13 public class MediaPanel extends JPanel 14 { 15 public MediaPanel( URL mediaURL ) 16 { 17 setLayout( new BorderLayout() ); // use a BorderLayout 18 19 // Use lightweight components for Swing compatibility 20 Manager.setHint( Manager.LIGHTWEIGHT_RENDERER, true ); 21 22 try 23 { 24 // create a player to play the media specified in the URL 25 Player mediaPlayer = Manager.createRealizedPlayer( mediaURL ); 26 27 // get the components for the video and the playback controls 28 Component video = mediaPlayer.getVisualComponent(); 29 Component controls = mediaPlayer.getControlPanelComponent(); 30 31 if ( video != null ) 32 add( video, BorderLayout.CENTER ); // add video component 33 34 if ( controls != null ) 35 add( controls, BorderLayout.SOUTH ); // add controls 36 37 mediaPlayer.start(); // start playing the media clip 38 } // end try 39 catch ( NoPlayerException noPlayerException ) 40 { 41 System.err.println( "No media player found" ); 42 } // end catch 43 catch ( CannotRealizeException cannotRealizeException ) 44 { 45 System.err.println( "Could not realize media player" ); 46 } // end catch 47 catch ( IOException iOException ) 48 { 49 System.err.println( "Error reading from the source" ); 50 } // end catch 51 } // end MediaPanel constructor 52 } // end class MediaPanel
import java.awt.*;import java.awt.event.*;import java.io.*;import javax.swing.*;import javax.media.*; public class MediaPlayerDemo extends JFrame { private Player player; private File file; public MediaPlayerDemo() { super( "Demonstrating the Java Media Player" ); JButton openFile = new JButton( "Open file to play" ); openFile.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { openFile(); createPlayer(); } } ); getContentPane().add( openFile, BorderLayout.NORTH ); setSize( 300, 300 ); show(); } private void openFile() { JFileChooser fileChooser = new JFileChooser(); fileChooser.setFileSelectionMode( JFileChooser.FILES_ONLY ); int result = fileChooser.showOpenDialog( this ); // user clicked Cancel button on dialog if ( result == JFileChooser.CANCEL_OPTION ) file = null; else file = fileChooser.getSelectedFile(); } private void createPlayer() { if ( file == null ) return; removePreviousPlayer(); try { // create a new player and add listener player = Manager.createPlayer( file.toURL() ); player.addControllerListener( new EventHandler() ); player.start(); // start player } catch ( Exception e ){ JOptionPane.showMessageDialog( this, "Invalid file or location", "Error loading file", JOptionPane.ERROR_MESSAGE ); } } private void removePreviousPlayer() { if ( player == null ) return; player.close(); Component visual = player.getVisualComponent(); Component control = player.getControlPanelComponent(); Container c = getContentPane(); if ( visual != null ) c.remove( visual ); if ( control != null ) c.remove( control ); } public static void main(String args[]) { MediaPlayerDemo app = new MediaPlayerDemo(); app.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { System.exit(0); } } ); } // inner class to handler events from media player private class EventHandler implements ControllerListener { public void controllerUpdate( ControllerEvent e ) { if ( e instanceof RealizeCompleteEvent ) { Container c = getContentPane(); // load Visual and Control components if they exist Component visualComponent = player.getVisualComponent(); if ( visualComponent != null ) c.add( visualComponent, BorderLayout.CENTER ); Component controlsComponent = player.getControlPanelComponent(); if ( controlsComponent != null ) c.add( controlsComponent, BorderLayout.SOUTH ); c.doLayout(); } } }}