Working with Movies

Get some popcorn—Java can play movies, with a little work. You’ll need to download and install 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. To use the JMF, add jmf.jar to your class path. Depending on what version of the JMF you download, the 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 types, like Apple QuickTime (.mov) and Windows Video (.avi). 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:

Player player = Manager.createPlayer(url);

Because video files are so large, and playing them requires significant system resources, Players have a multi-step lifecycle from the time they’re created to the time they actually play something. We’ll just look at one step, realizing. In this step, the Player finds out (by looking at the media file) what system resources it will need to actually 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 a Component that will show the media. The Player has to be realized first so that it knows important information, like how big the component should be. Getting the component is easy:

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 displays a movie in a JFrame and plays it:

//file: MoviePlayer.java
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import javax.swing.*;
import javax.media.*;

public class MoviePlayer extends JComponent {
  public static void main(String[] args) throws Exception {
    final JFrame f = new JFrame("MoviePlayer");
    f.addNotify( );
    f.setLocation(100, 100);
    f.addWindowListener(new WindowAdapter( ) {
      public void windowClosing(WindowEvent we) { System.exit(0); }
    });

    URL url = new URL(args[0]);
    final Player player = Manager.createPlayer(url);
    player.realize( );
    
    player.addControllerListener(new ControllerListener( ) {
      public void controllerUpdate(ControllerEvent ce) {
        if (ce instanceof RealizeCompleteEvent) {
          Component c = player.getVisualComponent( );
          Container content = f.getContentPane( );
          content.setLayout(new BorderLayout( ));
          content.add(c, BorderLayout.CENTER);
          Insets i = f.getInsets( );
          Dimension d = c.getSize( );
          f.setSize(d.width + i.left + i.right,
              d.height + i.top + i.bottom);
          f.setVisible(true);
          player.start( );
        }
      }
    });
  }
}

This class creates a JFrame that will hold the movie. 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 component and add it to the JFrame. Then we size the JFrame so that it exactly fits the movie component. Finally, we play the movie.

To use this class, pass the URL of a movie in the command line. I was able to show a movie that was on another machine in my local network like this:

java MoviePlayer http://172.16.0.1/the.english.patient.mov

Get Learning Java now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.