Demonstrate simple code to play a movie with Java Media Framework
/*
* Copyright (c) Ian F. Darwin, http://www.darwinsys.com/, 1996-2002.
* All rights reserved. Software written by Ian F. Darwin and others.
* $Id: LICENSE,v 1.8 2004/02/09 03:33:38 ian Exp $
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* Java, the Duke mascot, and all variants of Sun's Java "steaming coffee
* cup" logo are trademarks of Sun Microsystems. Sun's, and James Gosling's,
* pioneering role in inventing and promulgating (and standardizing) the Java
* language and environment is gratefully acknowledged.
*
* The pioneering role of Dennis Ritchie and Bjarne Stroustrup, of AT&T, for
* inventing predecessor languages C and C++ is also gratefully acknowledged.
*/
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.
*
* @author Ian F. Darwin, http://www.darwinsys.com/
* @version $Id: JMFPlayer.java,v 1.9 2004/02/09 03:21:20 ian Exp $
*/
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. */
public JMFPlayer(JFrame pf, String media) {
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
}
/** Called to stop the audio, as from a Stop button or menuitem */
public void stop() {
if (thePlayer == null)
return;
thePlayer.stop(); // stop playing!
thePlayer.deallocate(); // free system resources
}
/** Called when we are really finished (as from an Exit button). */
public void destroy() {
if (thePlayer == null)
return;
thePlayer.close();
}
/** Called by JMF when the Player has something to tell us about. */
public synchronized void controllerUpdate(ControllerEvent event) {
// 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 static void main(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);
}
}
Related examples in the same category