/* * Copyright (C) 1996-1997 Intel Corporation * All Rights Reserved * * A royalty-free copyright license to copy, modify and compile this source * code and to distribute it in your own products in source and binary forms * is hereby granted, so long as this copyright notice and this copyright * license appear in all source code copies. No other rights or licenses * are granted. The Intel name or other trademarks may not be used to * endorse or promote products using this header file without Intel's prior * written permission. * * THIS SOURCE CODE IS PROVIDED "AS IS" WITH NO WARRANTIES WHATSOEVER, * INCLUDING ANY WARRANTY OF MERCHANTABILITY, NONINFRINGEMENT, OR FITNESS * FOR ANY PARTICULAR PURPOSE. Intel disclaims all liability, including * liability for infringement of any proprietary rights, relating to use * of this source code. */ package FrameworkTutorial; import java.applet.*; import java.awt.*; import java.net.*; import java.io.IOException; import java.media.*; /** * This is a Java Applet that demonstrates how to create a simple * media player with a media event listener. */ public class SimplePlayer extends Applet implements ControllerListener { Player player = null; // media player Component visualComponent = null; // component in which the video is playing /** * Read the applet file parameter and create the media player. */ public void init() { String mediaFile = null; // input file name from Applet parameter URL mediaURL = null; // base URL for the document containing the applet setLayout(new BorderLayout()); /** * Get the media filename info. * The applet tag should contain the path to the * source media file, relative to the applet. */ if ((mediaFile = getParameter("MediaFile")) == null) { System.err.println("Invalid media file parameter"); } else { try { // Create an url from the file name and the url to the // document containing this applet. mediaURL = new URL(getDocumentBase(), mediaFile); // Create an instance of an appropriate media player for this media type. player = Manager.createPlayer(mediaURL); // Add this applet as a listener for the media player events if(player != null) { player.addControllerListener(this); } else System.err.println("Could not create player for " + mediaURL); } catch (MalformedURLException e) { System.err.println("Invalid media file URL!"); } catch(IOException e) { System.err.println("IO exception creating player for" + mediaURL); } } } /** * Start media file playback. This method is called the first time * that the Applet runs and every time the user re-enters the page. * * Call prefetch() to prepare to start the player. Prefetch returns * immediately, so this method does not call player.start(). The * controllerUpdate() method will call player.start() once the * player is Prefetched. */ public void start() { int state; if (player != null) player.prefetch(); } /** * Stop media file playback and release resources before leaving * the page. */ public void stop() { if (player != null) { player.stop(); player.deallocate(); } } /** * This controllerUpdate method must be defined in order to implement * a ControllerListener interface. This method will be called whenever * there is a media event. */ public void controllerUpdate(ControllerEvent event) { // When the player is Realized, get the visual component // and add it to the Applet if (event instanceof RealizeCompleteEvent) { if ((visualComponent = player.getVisualComponent()) != null) add("Center", visualComponent); // force the applet to draw the component validate(); } // Once the player has Prefetched, start it else if (event instanceof PrefetchCompleteEvent) { player.start(); } } }