Java, through its class libraries, provides extensive multimedia facilities that enable you to develop powerful multimedia applications. In this tutorial, we show how to play video and other media with the Java Media Framework. This tutorial is intended for students and developers who are familiar with Java GUIs and event handling.

21.6 Playing Video and Other Media with the Java Media Framework

A simple video can concisely and effectively convey a great deal of information. Recognizing the value of bringing extensible multimedia capabilities to Java, Sun Microsystems, Intel and Silicon Graphics worked together to produce the multimedia API Java Media Framework (JMF), discussed briefly in Section 21.1. Using the JMF API, programmers can create Java applications that play, edit, stream and capture many popular media types. While the features of JMF are quite extensive, this section briefly introduces some popular media formats and demonstrates playing video using the JMF API.

The JMF Web site provides versions of the JMF that take advantage of the performance features of certain platforms. For example, the JMF Windows Performance Pack provides extensive media and device support for Java programs running on Microsoft Windows platforms (Windows 95/98/NT 4.0/2000/XP). JMF's official Web site (java.sun.com/products/java-media/jmf) provides continually updated support, information and resources for JMF programmers.

Once the file finishes downloading, open it and follow the on-screen instructions to install the program. Leave all options at their defaults. You may need to restart your computer to finish the installation.

Creating a Simple Media Player

The JMF offers several mechanisms for playing media. The simplest mechanism is using objects that implement interface Player declared in package javax.media. Package javax.media and its subpackages contain the classes that compose the Java Media Framework. To play a media clip you must first create a URL object that refers to it. Then pass the URL as an argument to static method createRealizedPlayer of class Manager to obtain a Player for the media clip. Class Manager declares utility methods for accessing system resources to play and to manipulate media. Figure 21.6 declares a JPanel that demonstrates some of these methods.

The constructor (lines 1551) sets up the JPanel to play the media file specified as a URL parameter to the constructor. MediaPanel uses a BorderLayout (line 17). Line 20 invokes static method setHint to set the flag Manager.LIGHTWEIGHT_RENDER to true. This instructs the Manager to use a lightweight renderer that is compatible with lightweight Swing components, as opposed to the default heavyweight renderer. Inside the try block (lines 2238), line 25 invokes static method createRealizedPlayer of class Manager to create and realize a Player that plays the media file. When a Player realizes, it identifies the system resources it needs to play the media. Depending on the file, realizing can be a resource-consuming and time-consuming process. Method createRealizedPlayer throws three checked exceptions, NoPlayerException, CannotRealizeException and IOException. A NoPlayerException indicates that the system could not find a player that can play the file format. A CannotRealizeException indicates that the system could not properly identify the resources a media file needs. An IOException indicates that there was an error while reading the file. These exceptions are handled in the catch block in lines 3950.

Line 28 invokes method getVisualComponent of Player to get a Component that displays the visual (generally video) aspect of the media file. Line 29 invokes method getControlPanelComponent of Player to get a Component that provides playback and media controls. These components are assigned to local variables video and control, respectively. The if statements in lines 3132 and lines 3435 add the video and the controls if they exist. The videoComponent is added to the CENTER region (line 32), so it fills any available space on the JPanel. The controlsComponent, which is added to the SOUTH region, typically provides the following controls:

A positioning slider to jump to certain points in the media clip.

A pause button.

A volume button that provides volume control by right clicking and a mute function by left clicking.

A media properties button that provides detailed media information by left clicking and frame rate control by right clicking.

Line 37 calls Player method start to begin playing the media file. Lines 3950 handle the various exceptions that createRealizedPlayer throws.