/* * 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 and default controls. * When the media reaches the end the player will restart the media * from the beginning. */ public class ExtendedPlayer2 extends Applet implements ControllerListener { Player player = null; // media player Component visualComponent = null; // component in which the video is playing Component controlComponent = null; // component which controls the player gain, position, start and stop boolean running = false; // indicates if the applet is currently running, because // the user is on the page /** * 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. * */ public synchronized void start() { if (player != null){ player.start(); running = true; } } /** * Stop media file playback and release resources before leaving * the page. */ public synchronized void stop() { if (player != null) { player.stop(); player.deallocate(); running = false; } } /** * 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 synchronized void controllerUpdate(ControllerEvent event) { // do nothing if player is set to null if(player == null) return; // When the player is Realized, get the visual component // and control component and add them to the Applet if (event instanceof RealizeCompleteEvent) { if ((visualComponent = player.getVisualComponent()) != null) add("Center", visualComponent); if ((controlComponent = player.getControlPanelComponent()) != null) if (visualComponent != null) add("South",controlComponent); else add("Center",controlComponent); // force the applet to draw the components validate(); } // If this is the end of the media, "rewind" it to the beginning. else if (event instanceof EndOfMediaEvent) { player.setMediaTime(0); if(running == true) player.start(); } // A fatal player error has occurred else if (event instanceof ControllerErrorEvent) { player = null; System.err.println("FATAL ERROR: " + ((ControllerErrorEvent)event).getMessage()); } } }