O'Reilly logo

Learning Java, 4th Edition by Patrick Niemeyer, Daniel Leuck

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Java Media Framework

Get some popcorn—Java 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 Oracle’s website. 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 lifecycle 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 determines (by looking at the media file) the system resources that it needs to play the media file.


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):


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.add( visual, BorderLayout.CENTER );
                   frame.add( control, BorderLayout.SOUTH );
                   frame.setVisible( true );


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 MediaPlayerhttp://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.

Image of the dancing baby AVI

Figure 21-7. Image of the dancing baby AVI

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required