Displaying a Moving Image with Video

Problem

You want to display a video file within a Java program.

Solution

Use the Java Media Framework (JMF), a standard extension.

Discussion

Example 12-3 shows a program that displays a movie or other media file named on the command line. JMF is very flexible; this program will display (that is, play) an audio file with a volume control if the media object that you name contains a sound clip instead of a movie. Figure 12-4 shows JMFPlayer displaying a sound file and a movie.

JMFPlayer in action: audio (left), video (right)

Figure 12-4. JMFPlayer in action: audio (left), video (right)

Example 12-3. JMFPlayer.java

import com.darwinsys.util.WindowCloser; import java.applet.*; import java.awt.*; import javax.swing.*; import java.net.*; import java.io.*; import java.util.*; import javax.media.*; /** * Demonstrate simple code to play a movie with Java Media Framework. */ public class JMFPlayer extends JPanel implements ControllerListener { /** The player object */ Player thePlayer = null; /** The parent Frame we are in. */ JFrame parentFrame = null; /** Our contentpane */ Container cp; /** The visual component (if any) */ Component visualComponent = null; /** The default control component (if any) */ Component controlComponent = null; /** The name of this instance's media file. */ String mediaName; /** The URL representing this media file. */ URL theURL; /** Construct the player object and the GUI. ...

Get Java Cookbook 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.