Playing Video and Other Media with 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 specificationversion 2.0. Sun also provides a reference implementation of the JMF specificationJMF 2.1.1ethat 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.

Figure 21.6. JPanel that plays a media file from a URL.

(This item is displayed on page 995 in the print version)

 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

The constructor (lines 1551) 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 2238), 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 3950.

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 3132 and lines 3435 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 3950 handle the various exceptions that createRealizedPlayer tHRows.

The application in Fig. 21.7 displays a JFileChooser dialog for the user to choose a media file. It then creates a MediaPanel that plays the selected file and creates a JFrame to display the MediaPanel.s

Figure 21.7. Test application that creates a MediaPanel from a user-selected file.

(This item is displayed on pages 996 - 997 in the print version)

 1 // Fig. 21.7: MediaTest.java
 2 // A simple media player
 3 import java.io.File;
 4 import java.net.MalformedURLException;
 5 import java.net.URL;
 6 import javax.swing.JFileChooser;
 7 import javax.swing.JFrame;
 8
 9 public class MediaTest
10 {
11 // launch the application
12 public static void main( String args[] )
13 {
14 // create a file chooser
15 JFileChooser fileChooser = new JFileChooser();
16
17 // show open file dialog
18 int result = fileChooser.showOpenDialog( null );
19
20 if ( result == JFileChooser.APPROVE_OPTION ) // user chose a file
21 {
22 URL mediaURL = null;
23
24 try
25 {
26 // get the file as URL
27 mediaURL = fileChooser.getSelectedFile().toURL();
28 } // end try
29 catch ( MalformedURLException malformedURLException )
30 {
31 System.err.println( "Could not create URL for the file" );
32 } // end catch
33
34 if ( mediaURL != null ) // only display if there is a valid URL
35 {
36 JFrame mediaTest = new JFrame( "Media Tester" );
37 mediaTest.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
38
39 MediaPanel mediaPanel = new MediaPanel( mediaURL );
40 mediaTest.add( mediaPanel );
41
42 mediaTest.setSize( 300, 300 );
43 mediaTest.setVisible( true );
44 } // end inner if
45 } // end outer if
46 } // end main
47 } // end class MediaTest
 

Method main (lines 1246) assigns a new JFileChooser to local variable fileChooser (line 15), shows an open file dialog (line 18) and assigns the return value to result. Line 20 checks result to determine whether the user chose a file. To create a Player to play the selected media file, you must convert the File object returned by JFileChooser to a URL object. Method toURL of class File returns a URL that points to the File on the system, possibly throwing a MalformedURLException if it cannot create a URL object for the File. The try statement (lines 2432) creates a URL for the selected file and assigns it to mediaURL. The if statement in lines 3444 checks that mediaURL is not null and creates the GUI components to play the media.

Introduction to Computers, the Internet and the World Wide Web

Introduction to Java Applications

Introduction to Classes and Objects

Control Statements: Part I

Control Statements: Part 2

Methods: A Deeper Look

Arrays

Classes and Objects: A Deeper Look

Object-Oriented Programming: Inheritance

Object-Oriented Programming: Polymorphism

GUI Components: Part 1

Graphics and Java 2D™

Exception Handling

Files and Streams

Recursion

Searching and Sorting

Data Structures

Generics

Collections

Introduction to Java Applets

Multimedia: Applets and Applications

GUI Components: Part 2

Multithreading

Networking

Accessing Databases with JDBC

Servlets

JavaServer Pages (JSP)

Formatted Output

Strings, Characters and Regular Expressions

Appendix A. Operator Precedence Chart

Appendix B. ASCII Character Set

Appendix C. Keywords and Reserved Words

Appendix D. Primitive Types

Appendix E. (On CD) Number Systems

Appendix F. (On CD) Unicode®

Appendix G. Using the Java API Documentation

Appendix H. (On CD) Creating Documentation with javadoc

Appendix I. (On CD) Bit Manipulation

Appendix J. (On CD) ATM Case Study Code

Appendix K. (On CD) Labeled break and continue Statements

Appendix L. (On CD) UML 2: Additional Diagram Types

Appendix M. (On CD) Design Patterns

Appendix N. Using the Debugger

Inside Back Cover



Java(c) How to Program
Java How to Program (6th Edition) (How to Program (Deitel))
ISBN: 0131483986
EAN: 2147483647
Year: 2003
Pages: 615

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