/* * 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. */ import java.applet.Applet; import java.awt.*; import java.lang.String; import java.net.URL; import java.net.MalformedURLException; 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. It will play the media * clip right away and continuously loop. * * */ public class MediaValidationPlayer 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 Component progressBar = null; boolean running = false; // indicates if the player should be running // right now, 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 URL codeBase = null; // URL to the source media file setLayout(new BorderLayout()); // Get the media filename info. // The applet tag should contain the path to the // source media file, relative to the html page. codeBase = this.getDocumentBase(); if ((mediaFile = getParameter("FILE")) == 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(codeBase, 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 function 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 function must be defined in order to implement * a ControllerListener interface. This function will be called whenever * there is a media event. */ public synchronized void controllerUpdate(ControllerEvent event) { if(player == null) return; // When the player is Realized, get the visual // and control components and add them to the Applet if (event instanceof RealizeCompleteEvent) { setLayout(new BorderLayout()); 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 we've reached the end of the media, // rewind it to the beginning. else if (event instanceof EndOfMediaEvent) { player.setMediaTime(0); synchronized (this){ if(running == true) player.start(); } } else if (event instanceof CachingControlEvent) { // Put a progress bar up when downloading starts, // take it down when downloading ends. CachingControlEvent e = (CachingControlEvent) event; CachingControl cc = e.getCachingControl(); long cc_progress = e.getContentProgress(); long cc_length = cc.getContentLength(); if (progressBar == null) // Add the bar if not already there ... if ((progressBar = cc.getProgressBarComponent()) != null) { add("North", progressBar); validate(); } if (progressBar != null) // Remove bar when finished ownloading if (cc_progress == cc_length) { remove (progressBar); progressBar = null; validate(); } } else if (event instanceof ControllerErrorEvent) { // A fatal player error has occurred player = null; System.err.println("FATAL ERROR: " + ((ControllerErrorEvent)event).getMessage()); } } }