Recipe 13.10 Playing a Video Clip


Problem

You want to display a video file within a Java program.

Solution

Use the Java Media Framework (JMF), a standard extension.

Discussion

Example 13-5 shows a program that displays a movie or other media file named on the command line. JMF is very flexible; this program plays an audio file, supplying a volume control if the media object that you name contains a sound clip instead of a movie. Figure 13-6 shows JMFPlayer displaying a sound file and a movie.

Figure 13-6. JMFPlayer in action: audio (left), video (right)
figs/jcb2_1306.gif


Example 13-5. JMFPlayer.java
import com.darwinsys.util.WindowCloser; import java.applet.*; import java.awt.*; import javax.swing.*; import java.net.*; import java.io.*; import java.util.*; import javax.media.*; /**  * Demonstrate simple code to play a movie with Java Media Framework.  */ public class JMFPlayer extends JPanel implements ControllerListener {     /** The player object */     Player thePlayer = null;     /** The parent Frame we are in. */     JFrame parentFrame = null;     /** Our contentpane */     Container cp;     /** The visual component (if any) */     Component visualComponent = null;     /** The default control component (if any) */     Component controlComponent = null;     /** The name of this instance's media file. */     String mediaName;     /** The URL representing this media file. */     URL theURL;     /** Construct the player object and the GUI. */     public JMFPlayer(JFrame pf, String media) {         parentFrame = pf;         mediaName = media;         // cp = getContentPane( );         cp = this;         cp.setLayout(new BorderLayout( ));         try {             theURL = new URL(getClass( ).getResource("."), mediaName);             thePlayer = Manager.createPlayer(theURL);             thePlayer.addControllerListener(this);         } catch (MalformedURLException e) {             System.err.println("JMF URL creation error: " + e);         } catch (Exception e) {             System.err.println("JMF Player creation error: " + e);             return;         }         System.out.println("theURL = " + theURL);         // Start the player: this will notify our ControllerListener.         thePlayer.start( );        // start playing     }     /** Called to stop the audio, as from a Stop button or menuitem */     public void stop( ) {         if (thePlayer == null)             return;         thePlayer.stop( );        // stop playing!         thePlayer.deallocate( );    // free system resources     }     /** Called when we are really finished (as from an Exit button). */     public void destroy( ) {         if (thePlayer == null)             return;         thePlayer.close( );     }     /** Called by JMF when the Player has something to tell us about. */     public synchronized void controllerUpdate(ControllerEvent event) {         // System.out.println("controllerUpdate(" + event + ")");         if (event instanceof RealizeCompleteEvent) {             if ((visualComponent = thePlayer.getVisualComponent( )) != null)                     cp.add(BorderLayout.CENTER, visualComponent);             if ((controlComponent =                  thePlayer.getControlPanelComponent( )) != null)                     cp.add(BorderLayout.SOUTH, controlComponent);             // re-size the main window             if (parentFrame != null) {                 parentFrame.pack( );                 parentFrame.setTitle(mediaName);             }         }     }     public static void main(String[] argv) {         JFrame f = new JFrame("JMF Player Demo");         Container frameCP = f.getContentPane( );         JMFPlayer p = new JMFPlayer(f, argv.length == 0 ?             "file:///C:/music/midi/beet5th.mid" : argv[0]);         frameCP.add(BorderLayout.CENTER, p);         f.setSize(200, 200);         f.setVisible(true);         f.addWindowListener(new WindowCloser(f, true));     } }

The optional Java Media Framework includes much more functionality than this example shows. However, the ability to display a QuickTime or MPEG movie with only a few lines of code is one of JMF's most endearing young charms. We load the media file from a URL and create a Player object to manage it. If it makes sense for the given player to have a controller, it will have one, and we add it to the bottom of the applet. Controllers may include volume controls, forward/backward buttons, position sliders, etc. However, we don't have to care: we get a component that contains all the appropriate controls for the kind of media clip for which we've created the player. If the given player represents a medium with a visual component (like a movie or a bitmap image), we add this to the center of the applet.

See Also

Of course, there is much more to the JMF API than this. You can, for example, coordinate playing of audio and video with each other or with other events.



Java Cookbook
Java Cookbook, Second Edition
ISBN: 0596007019
EAN: 2147483647
Year: 2003
Pages: 409
Authors: Ian F Darwin

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net