Sun Java Solaris Communities My SDN Account Join SDN

1.0 Programmers Guide

1.0 Overview

 
TOC  Prev  Next  
The sample program PlayerApplet demonstrates how to create a Java Media Player and present an MPEG movie from within a Java applet. This is a general example that could easily be adapted to present other types of media streams.

The Player's visual presentation and its controls are displayed within the applet's presentation space in the browser window. If you create a Player in a Java application, you are responsible for creating the window to display the Player's components.

Note: While PlayerApplet illustrates the basic usage of a Java Media Player, it does not perform the error handling necessary in a real applet or application. For a more complete sample suitable for use as a template, see "Appendix A: Java Media Applet" on page 37.

2.1 Overview of PlayerApplet

The APPLET tag is used to invoke PlayerApplet in an HTML file. The WIDTH and HEIGHT fields of the HTML APPLET tag determine the dimensions of the applet's presentation space in the browser window. The PARAM tag identifies the media file to be played. For example, PlayerApplet could be invoked with:


<APPLET CODE=ExampleMedia.PlayerApplet
WIDTH=320 HEIGHT=300> <PARAM NAME=FILE VALUE="Astrnmy.mpg"> </APPLET>
When a user opens a web page containing PlayerApplet, the applet loads automatically and runs in the specified presentation space, which contains the Player's visual component and default controls. The Player starts and plays the MPEG movie once. The user can use the default Player controls to stop, restart, or replay the movie. If the page containing the applet is closed while the Player is playing the movie, the Player automatically stops and frees the resources it was using.

To accomplish this, PlayerApplet extends Applet and implements the ControllerListener interface. PlayerApplet defines five methods:

  • init-creates a Player for the file that was passed in through the PARAM tag and registers PlayerApplet as a controller listener so that it can observe media events posted by the Player. (This causes PlayerApplet's controllerUpdate method to be called whenever the Player posts an event.)

  • start-starts the Player when PlayerApplet is started.

  • stop-stops and deallocates the Player when PlayerApplet is stopped.

  • destroy-closes the Player when PlayerApplet is removed.

  • controllerUpdate-responds to Player events to display the Player's components.

2.2 PlayerApplet Code Listing


PlayerApplet.java: package ExampleMedia; import java.applet.*; import java.awt.*; import java.net.*; import javax.media.*; public class PlayerApplet extends Applet implements ControllerListener { Player player = null; public void init() { setLayout(new BorderLayout()); String mediaFile = getParameter("FILE"); try { URL mediaURL = new URL(getDocumentBase(), mediaFile); player = Manager.createPlayer(mediaURL); player.addControllerListener(this); } catch (Exception e) { System.err.println("Got exception "+e); } } public void start() { player.start(); } public void stop() { player.stop(); player.deallocate(); } public void destroy() { player.close(); } public synchronized void controllerUpdate(ControllerEvent event) { if (event instanceof RealizeCompleteEvent) { Component comp; if ((comp = player.getVisualComponent()) != null) add ("Center", comp); if ((comp = player.getControlPanelComponent()) != null) add ("South", comp); validate(); } } }

2.3 Initializing the Applet

When a Java applet starts, its init method is invoked automatically. You override init to prepare your applet to be started. PlayerApplet performs four tasks in init:

  1. Retrieves the applet's FILE parameter.
  2. Uses the FILE parameter to locate the media file and build a URL object that describes that media file.
  3. Creates a Player for the media file by calling Manager.createPlayer.
  4. Registers the applet as a controller listener with the new Player by calling addControllerListener. Registering as a listener causes PlayerApplet's controllerUpdate method to be called automatically whenever the Player posts a media event. The Player posts media events whenever its state changes. This mechanism allows you to control the Player's transitions between states and ensure that the Player is in a state in which it can process your requests. (For more information, see "Player States" on page 6.)

public void init() { setLayout(new BorderLayout()); // 1. Get the FILE parameter. String mediaFile = getParameter("FILE"); try { // 2. Create a URL from the FILE parameter. The URL // class is defined in java.net. URL mediaURL = new URL(getDocumentBase(), mediaFile); // 3. Create a player with the URL object. player = Manager.createPlayer(mediaURL); // 4. Add PlayerApplet as a listener on the new player. player.addControllerListener(this); } catch (Exception e) { System.err.println("Got exception "+e); } }

2.4 Controlling the Player

The Applet class defines start and stop methods that are called automatically when the page containing the applet is opened and closed. You override these methods to define what happens each time your applet starts and stops.

PlayerApplet implements start to start the Player whenever the applet is started:


public void start() { player.start(); }
Similarly, PlayerApplet overrides stop to stop and deallocate the Player:


public void stop() { player.stop(); player.deallocate(); }
Deallocating the Player releases any resources that would prevent another Player from being started. For example, if the Player uses a hardware device to present its media, deallocate frees that device so that other Players can use it.

When an applet exits, destroy is called to dispose of any resources created by the applet. PlayerApplet overrides destroy to close the Player. Closing a Player releases all of the resources that it's using and shuts it down permanently.


public void destroy() { player.close(); }

2.5 Responding to Media Events

PlayerApplet registers itself as a ControllerListener in its init method so that it receives media events from the Player. To respond to these events, PlayerApplet implements the controllerUpdate method, which is called automatically when the Player posts an event.

PlayerApplet responds to one type of event, RealizeCompleteEvent. When the Player posts a RealizeCompleteEvent, PlayerApplet displays the Player's components:


public synchronized void controllerUpdate(ControllerEvent event) { if (event instanceof RealizeCompleteEvent) { Component comp; if ((comp = player.getVisualComponent()) != null) add ("Center", comp); if ((comp = player.getControlPanelComponent()) != null) add ("South", comp); validate(); }

A Player's user-interface components cannot be displayed until the Player is Realized; an Unrealized Player doesn't know enough about its media stream to provide access to its user-interface components. PlayerApplet waits for the Player to post a RealizeCompleteEvent and then displays the Player's visual component and default control panel by adding them to the applet container. Calling validate triggers the layout manager to update the display to include the new components.

TOC  Prev  Next