O'Reilly logo

Java Cookbook by Ian F. Darwin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required