Deitel & Associates, Inc. Logo

Back to www.deitel.com
digg.png delicious.png blinkit.png furl.png
Java How to Program, 6/e

ISBN:
0-13-148398-6
© 2005
pages: 1576

Buy the Book!
Amazon logo
InformIT logo

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.

Download the code examples for this tutorial.

[Note: This tutorial is an excerpt (Section 21.6) of Chapter 21, Multimedia, from our textbook Java How to Program, 6/e. This tutorial may refer to other chapters or sections of the book that are not included here. Permission Information: Deitel, Harvey M. and Paul J., JAVA HOW TO PROGRAM, ©2005, pp.992-996. Electronically reproduced by permission of Pearson Education, Inc., Upper Saddle River, New Jersey.]

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.

IBM and Sun developed the latest JMF specification—version 2.0. Sun also provides a reference implementation of the JMF specification—JMF 2.1.1e—that supports media file types such as Microsoft Audio/Video Interleave (.avi), Macromedia Flash 2 movies (.swf), Future Splash (.spl), MPEG Layer 3 Audio (.mp3), Musical Instrument Digital Interface (MIDI; .mid or .rmi extensions), MPEG-1 videos (.mpeg, .mpg), QuickTime (.mov), Sun Audio file format (.au extension), and Macintosh AIFF file format (.aif or .aiff extension). You have already seen some of these files types.

Currently, JMF is available as an extension separate from the Java 2 Software Development Kit. The most recent JMF implementation (2.1.1e) can be downloaded from:

http://java.sun.com/products/java-media/jmf/2.1.1/download.html

You need to accept the license agreement prior to downloading.

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 video Component is added to the CENTER region (line 32), so it fills any available space on the JPanel. The controls Component, which is added to the SOUTH region, typically provides the following controls:

  1. A positioning slider to jump to certain points in the media clip.
  2. A pause button.
  3. A volume button that provides volume control by right clicking and a mute function by left clicking.
  4. 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.

Fig. 21.6 | JPanel that plays a media file from a URL.
1     // Fig. 21.6: MediaPanel.java
2     // A JPanel the plays media from a URL
3     import java.awt.BorderLayout;
4     import java.awt.Component;
5     import java.io.IOException;
6     import java.net.URL;
7     import javax.media.CannotRealizeException;
8     import javax.media.Manager;
9     import javax.media.NoPlayerException;
10    import javax.media.Player;
11    import javax.swing.JPanel;
12   
13    public class MediaPanel extends JPanel
14    {
15       public MediaPanel( URL mediaURL )
16       {
17          setLayout( new BorderLayout() ); // use a BorderLayout
18   
19          // Use lightweight components for Swing compatibility
20    Manager.setHint( Manager.LIGHTWEIGHT_RENDERER, true );
21         
22          try
23          {
24             // create a player to play the media specified in the URL
25             Player mediaPlayer = Manager.createRealizedPlayer( mediaURL );
26   
27             // get the components for the video and the playback controls
28             Component video = mediaPlayer.getVisualComponent();
29             Component controls = mediaPlayer.getControlPanelComponent();
30            
31             if ( video != null )
32                add( video, BorderLayout.CENTER ); // add video component
33   
34             if ( controls != null )
35                add( controls, BorderLayout.SOUTH ); // add controls
36   
37             mediaPlayer.start(); // start playing the media clip
38          } // end try
39          catch ( NoPlayerException noPlayerException )
40          {
41             System.err.println( "No media player found" );
42          } // end catch
43          catch ( CannotRealizeException cannotRealizeException )
44    {
45             System.err.println( "Could not realize media player" );
46          } // end catch
47          catch ( IOException iOException )
48    {
49             System.err.println( "Error reading from the source" );
50          } // end catch
51       } // end MediaPanel constructor
52    } // end class MediaPanel
 
Next Page
Return to Tutorial Index