FileDocCategorySizeDatePackage
JMFPlayer.javaAPI DocExample2956Sun Feb 08 21:21:20 GMT 2004None

JMFPlayer

public class JMFPlayer extends JPanel implements ControllerListener
Demonstrate simple code to play a movie with Java Media Framework.
author
Ian F. Darwin, http://www.darwinsys.com/
version
$Id: JMFPlayer.java,v 1.9 2004/02/09 03:21:20 ian Exp $

Fields Summary
Player
thePlayer
The player object
JFrame
parentFrame
The parent Frame we are in.
Container
cp
Our contentpane
Component
visualComponent
The visual component (if any)
Component
controlComponent
The default control component (if any)
String
mediaName
The name of this instance's media file.
URL
theURL
The URL representing this media file.
Constructors Summary
public JMFPlayer(JFrame pf, String media)
Construct the player object and the GUI.


	        
	     
		parentFrame = pf;
		mediaName = media;
		// cp = getContentPane();
		cp = this;
		cp.setLayout(new BorderLayout());
		try {
			theURL = new URL(getClass().getResource("."), mediaName);
			thePlayer = Manager.createPlayer(theURL);
			thePlayer.addControllerListener(this);
		} catch (MalformedURLException e) {
			System.err.println("JMF URL creation error: " + e);
		} catch (Exception e) {
			System.err.println("JMF Player creation error: " + e);
			return;
		}
		System.out.println("theURL = " + theURL);

		// Start the player: this will notify our ControllerListener.
		thePlayer.start();		// start playing
	
Methods Summary
public synchronized voidcontrollerUpdate(javax.media.ControllerEvent event)
Called by JMF when the Player has something to tell us about.

		// System.out.println("controllerUpdate(" + event + ")");
		if (event instanceof RealizeCompleteEvent) {
			if ((visualComponent = thePlayer.getVisualComponent()) != null)
					cp.add(BorderLayout.CENTER, visualComponent);
			if ((controlComponent = 
				thePlayer.getControlPanelComponent()) != null)
					cp.add(BorderLayout.SOUTH, controlComponent);
			// re-size the main window
			if (parentFrame != null) {
				parentFrame.pack();
				parentFrame.setTitle(mediaName);
			}
		}
	
public voiddestroy()
Called when we are really finished (as from an Exit button).

		if (thePlayer == null)
			return;
		thePlayer.close();
	
public static voidmain(java.lang.String[] argv)

		JFrame f = new JFrame("JMF Player Demo");
		Container frameCP = f.getContentPane();
		JMFPlayer p = new JMFPlayer(f, argv.length == 0 ?
			"file:///C:/music/midi/beet5th.mid" : argv[0]);
		frameCP.add(BorderLayout.CENTER, p);
		f.setSize(200, 200);
		f.setVisible(true);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	
public voidstop()
Called to stop the audio, as from a Stop button or menuitem

		if (thePlayer == null)
			return;
		thePlayer.stop();		// stop playing!
		thePlayer.deallocate();	// free system resources