/* * 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 add your own * custom controls to the basic media player. */ public class ExtendedPlayer3 extends Applet implements ControllerListener { Player player = null; // media player Component visualComponent = null; // component in which the video is playing boolean running = false; // indicates if the applet is currently running, because // the user is on the page /** * The component of the media player that holds the gainControl reference. */ GainControl gainControl = null; /** * Panel used to hold the custom controls in the Applets Layout Manager. */ Panel controlPanel = null; /** * Runnable class used to monitor media progress and update the UI. */ MediaProgressMonitor progressMonitor = null; /** * Buttons within the custom controls used to adjust Starting, Stopping, * Gain increase and Gain decrease. */ Button startButton = null; Button stopButton = null; Button gainUpButton = null; Button gainDownButton = null; /** * Labels within the custom controls used to indicate adjustable media player * features. */ Label controlLabel = null; Label gainLabel = null; Label muteLabel = null; Label mediaTimesLabel = null; /** * Checkbox within the custom controls used control the Mute feature of the * media player. */ Checkbox muteCheckbox = null; /** * Textfield within the custom controls used to indicate the current media player * position and total media file duration. */ TextField timeText = null; /** * 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 synchronized void start() { if (player != null){ player.prefetch(); running = true; } } /** * Stop media file playback and release resources before leaving * the page. */ public synchronized void stop() { if (player != null) { progressMonitor.stop(); 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); // Get pointer to the Gain Control of the media player. gainControl = player.getGainControl(); // Create the custom control components. createCustomControls(); if (visualComponent != null) add("South",controlPanel); else add("Center",controlPanel); // Create the duration monitor object: progressMonitor = new MediaProgressMonitor(this); // force the applet to draw the components validate(); } // Once the player has Prefetched, start it else if (event instanceof PrefetchCompleteEvent) { if(running) player.start(); progressMonitor.start(); } // If we've reached the end of the media "rewind" it to the beginning. else if (event instanceof EndOfMediaEvent) { player.setMediaTime(0); if(running) player.start(); } // A fatal player error has occurred else if (event instanceof ControllerErrorEvent) { progressMonitor.stop(); player = null; System.err.println("FATAL ERROR: " + ((ControllerErrorEvent)event).getMessage()); } } /** * This method handles the AWT details required to display * player controls. */ public void createCustomControls() { controlPanel = new Panel(); GridBagLayout gridBag = new GridBagLayout(); GridBagConstraints gridBagCon = new GridBagConstraints(); controlPanel.setFont(new Font("Arial", Font.PLAIN, 14)); controlPanel.setLayout(gridBag); // Create the first row of AWT control components: Label, Start Button, Stop Button. gridBagCon.fill = GridBagConstraints.BOTH; gridBagCon.weightx = 1.0; controlLabel = new Label("Controls:", Label.LEFT); makeControl(controlPanel, controlLabel, gridBag, gridBagCon); startButton = new Button("Start"); makeControl(controlPanel, startButton, gridBag, gridBagCon); gridBagCon.gridwidth = GridBagConstraints.REMAINDER; stopButton = new Button("Stop"); makeControl(controlPanel, stopButton, gridBag, gridBagCon); // Create the second row of AWT control components: Label, GainUp Button, GainDown Button. gridBagCon.weightx = 1.0; gridBagCon.gridwidth = 1; gainLabel = new Label("Gain:", Label.LEFT); makeControl(controlPanel, gainLabel, gridBag, gridBagCon); gainUpButton = new Button("+"); makeControl(controlPanel, gainUpButton, gridBag, gridBagCon); gridBagCon.gridwidth = GridBagConstraints.REMAINDER; gainDownButton = new Button("-"); makeControl(controlPanel, gainDownButton, gridBag, gridBagCon); // Create the third row of AWT control components: Label, Mute checkbox. gridBagCon.gridwidth = GridBagConstraints.RELATIVE; gridBagCon.weightx = 1.0; muteLabel = new Label("Mute:", Label.LEFT); makeControl(controlPanel, muteLabel, gridBag, gridBagCon); gridBagCon.gridwidth = GridBagConstraints.REMAINDER; muteCheckbox = new Checkbox(""); makeControl(controlPanel, muteCheckbox, gridBag, gridBagCon); // Create the third row of AWT control components: Label, media time textbox. gridBagCon.gridwidth = 1; gridBagCon.weightx = 1.0; mediaTimesLabel = new Label("Current Time/Total Time:", Label.LEFT); makeControl(controlPanel, mediaTimesLabel, gridBag, gridBagCon); gridBagCon.gridwidth = GridBagConstraints.REMAINDER; timeText = new TextField("0.0//0.0", 12); timeText.setEditable(false); makeControl(controlPanel, timeText, gridBag, gridBagCon); } /** * This method adds a control to the custom control layout manager. */ protected void makeControl(Container parentComp, Component newComponent, GridBagLayout gridbag, GridBagConstraints constraint) { gridbag.setConstraints(newComponent, constraint); parentComp.add(newComponent); } /** * This method captures all the events from the custom controls. This * is where each controls is calls the media player control methods. */ public boolean action(Event evt, Object arg) { if (evt.target instanceof Button) { // Process the button event: if ("Start".equals(arg)) { player.start(); } else if ("Stop".equals(arg)) { player.stop(); } else if ("+".equals(arg)) { gainControl.setDB(2.0f); } else if ("-".equals(arg)) { gainControl.setDB(-2.0f); } return(true); } else if (evt.target instanceof Checkbox) { // Set the players Mute control based upon the checkbox state. if (muteCheckbox.getState() == true) gainControl.setMute(true); else gainControl.setMute(false); return(true); } else return(false); } } /** * This class is used to continually monitor the progress * of the playing media file. The thread wakes up every 50 millisec * and passes the progress info to the player controls in the applet. */ class MediaProgressMonitor implements Runnable { ExtendedPlayer3 applet = null; String mediaDuration = null; String mediaTime = null; Thread thread = null; public MediaProgressMonitor(ExtendedPlayer3 parent) { applet = parent; // Get the total time of the media file and store for use later. long duration = applet.player.getDuration(); mediaDuration = new String(String.valueOf(duration / (long) 1e08)); char tmpChar = mediaDuration.charAt(mediaDuration.length() - 1); mediaDuration = String.valueOf(duration / (long) 1e09) + "." + tmpChar; } /** * This method is called when the user starts the Applet or returns * from another page. */ public synchronized void start() { thread = new Thread(this); thread.start(); } /** * This method is called when the user stops the Applet or * leaves the page. */ public synchronized void stop() { thread.stop(); } /** * This method is called after the start method has executed. * Every 50 milliseconds, check the media player's progress * and forward the results to the player's control component. */ public void run() { while (true) { // Update the media time text field. long currtime = applet.player.getMediaTime(); mediaTime = new String(String.valueOf(currtime / (long) 1e08)); char tmpChar = mediaTime.charAt(mediaTime.length() - 1); mediaTime = String.valueOf(currtime / (long) 1e09) + "." + tmpChar; applet.timeText.setText(mediaTime + "/" + mediaDuration); try { thread.sleep(50); } catch (InterruptedException e) { System.err.println(e); } } } }