Section 21.6. Java Media Framework


21.6. Java Media Framework

Get some popcornJava can play movies! To do this, though, we'll need one of Java's standard extension APIs, the Java Media Framework (JMF). The JMF defines a set of interfaces and classes in the javax.media and javax.media.protocol packages. You can download the latest JMF from http://java.sun.com/products/java-media/jmf/. To use the JMF, add jmf.jar to your classpath. Or, depending on what version of the JMF you download, a friendly installation program may do this for you.

We'll only scratch the surface of JMF here, by working with an important interface called Player. Specific implementations of Player deal with different media "container" types, such as Apple QuickTime (.mov) and Windows Video (.avi). For a full list of supported media types and codecs, consult the latest JMF documentation. There are also players for audio types including MP3. Players are handed out by a high-level class in the JMF called Manager. One way to obtain a Player is to specify the URL of a movie.

What about Windows media player format as well as MP3?

     Player player = Manager.createPlayer(url); 

Because video files are so large and playing them requires significant system resources, Players have a multistep life cycle from the time they're created to the time they actually play something. We'll look at only one step, realizing. In this step, the Player finds out (by looking at the media file) what system resources it needs to play the media file.

     player.realize(  ); 

The realize( ) method returns right away; it kicks off the realizing process in a separate thread. When the player is finished realizing, it sends out an event. Once you receive this event, you can obtain one of two Components from the Player. The first is a visual component that, for visual media types, shows the media. The second is a control component that provides a prefab user interface for controlling the media presentation. The control normally includes start, stop, and pause buttons, along with volume controls and attendant goodies.

The Player has to be realized before you ask for these components so that it has important information, like how big the component should be. After that, getting the component is easy. Here's an example:

     Component c = player.getVisualComponent(  ); 

Now, we just need to add the component to the screen somewhere. We can play the media right away (although this actually moves the Player through several other internal states):

     player.start(  ); 

The following example, MediaPlayer, uses the JMF to load and display a movie or audio file from a specified URL:

     //file: MediaPlayer.java     import java.awt.*;     import java.net.URL;     import javax.swing.*;     import javax.media.*;     public class MediaPlayer     {        public static void main( String[] args ) throws Exception {           final JFrame frame = new JFrame("MediaPlayer");           frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );           URL url = new URL( args[0] );           final Player player = Manager.createPlayer( url );           player.addControllerListener( new ControllerListener(  ) {              public void controllerUpdate( ControllerEvent ce ) {                 if ( ce instanceof RealizeCompleteEvent )                 {                    Component visual = player.getVisualComponent(  );                    Component control = player.getControlPanelComponent(  );                    if ( visual != null )                       frame.getContentPane(  ).add( visual, "Center" );                    frame.getContentPane(  ).add( control, "South" );                    frame.pack(  );                    frame.setVisible( true );                    player.start(  );                 }              }           });           player.realize(  );        }     } 

This class creates a JFrame that holds the media. Then, it creates a Player from the URL specified on the command line and tells the Player to realize( ). There's nothing else we can do until the Player is realized, so the rest of the code operates inside a ControllerListener after the RealizeCompleteEvent is received.

In the event handler, we get the Player's visual and controller components and add them to the JFrame. We display the JFrame and, finally, we play the movie. It's very simple!

To use the MediaPlayer, pass it the URL of a movie or audio file on the command line. Here are a couple of examples:

     % java MediaPlayer file:dancing_baby.avi     % java MediaPlayer http://myserver/mp3s/TheCure/KissMe/catch.mp3 

Figure 21-7 shows the "dancing baby" AVI running in the MediaPlayer. Feel free to dance along, if you want.

Figure 21-7. Image of the dancing baby AVI




    Learning Java
    Learning Java
    ISBN: 0596008732
    EAN: 2147483647
    Year: 2005
    Pages: 262

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