Bare Bones Player Applet—A First Applet Using JMF

This final section introduces a simple bare bones applet that illustrates how simply features of the JMF can be used.

BBPApplet (Bare Bones Player Applet) is an applet that will play a single media object. The name of the media object (file) is specified as a parameter in the HTML file (within the applet tag) that contains the applet. The applet is written in a minimalist fashion: Only those necessary features of the JMF are employed, and nothing fancy is done with regard to GUI design. Subsequent examples in the next chapter will be more sophisticated.

Listing 7.4 is the source of the applet, which can also be found on the book's companion Web site (www.samspublishing.com). What should be clear is how small the applet is: It consists of just two methods, each of fewer than a dozen lines. Yet the result is an applet capable of playing any number of a range of different video and audio formats while also giving the user direct control over that playback through a control panel. Figure 7.16 shows the applet in action playing a synthetically generated video. Also shown are the Media Properties and PlugIn Viewer windows that were raised through the BBPApplet's controls.

Listing 7.4 BBPApplet (Bare Bones Player Applet)
/***************************************************************
* A "Bare Bones" Player Applet (BBP Applet) that will play
* the media object indicated in the "media2Play" property
* of the Applet tag.
*
*<p>The applet demonstrates the relative ease with which
* the JMF can be employed, particularly for playing. The
* applet is a minimal player, placing the controls for the
* player and the visual component for the played object
* within the Applet. The object plays once, but can be
* controlled by the user through the control panel provided.
*
*<p>The tag for the Applet should look something like:
*
* <!-- Sample HTML
* <applet code="BBPApplet.class" width=300 height=400>
* <param name="media2Play" value="myVideo.mpg">
* </applet>
* -->
*
*@author Spike Barlow
**************************************************************/

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.*;
import javax.media.*;
public class BBPApplet extends Applet implements ControllerListener {

    /*****************************************
    * Object to play the media. Only attribute
    * that the Applet really needs.
    ******************************************/
    protected Player    player;

    /******************************************
    * The name of the media to be played.
    *******************************************/
    protected String    nameOfMedia2Play;

    /**********************************************
    * Name of the Property field within the
    * applet tag indicating the name of the media
    * to play.
    ***********************************************/
    private static final String   MEDIA_NAME_PROPERTY = "media2Play";

    /***********************************************
    * Object describing the location of the media to
    * be played.
    ************************************************/
    protected MediaLocator      locator;



/***************************************************************
* Initialise the applet by attempting to create and start a
* Player object capable of playing the media specified in the
* applet tag.
*****************************************************************/
public void init() {

    setLayout(new BorderLayout());
    setBackground(Color.lightGray);
    try {
        nameOfMedia2Play = (new URL(getDocumentBase(),
                            getParameter(MEDIA_NAME_PROPERTY)
                            )).toExternalForm();
        locator = new MediaLocator(nameOfMedia2Play);
        player = Manager.createPlayer(locator);
        player.addControllerListener(this);
        player.start();
    }
    catch (Exception e) {
        throw new Error("Couldn't initialise BBPApplet: "
                + e.getMessage());
    }
}



/**************************************************************
* Respond to ControllerEvents from the Player that was created.
* For the bare bones player the only event of import is the
* RealizeCompleteEvent. At that stage the visual component and
* controller for the Player can finally be obtained and thus
* displayed.
****************************************************************/
public synchronized void controllerUpdate(ControllerEvent e) {

    if (e instanceof RealizeCompleteEvent) {
        add(player.getVisualComponent(),"North");
        add(player.getControlPanelComponent(),"South");
        validate();
    }

}

}

Figure 7.16. The BBPApplet (Bare Bones Player Applet) playing a synthetically generated video.


Several objects from the JMF API are employed in the applet. Chief among these is the Player object player. It is the only real attribute that the applet must possess because both methods need to refer to it. In order to create the Player object, a MediaLocator object locator is constructed from the user-specified filename that contains the media to be played. After that object is constructed, it is passed to the Manager class so that the Player can be created.

Several features typical of JMF programming can be found in this small example. The applet employs event-driven programming in order to determine when the Player is realized, and thus when a visual component and control panel for that Player can be obtained. Further, try/catch blocks are used to enclose the code in the init() method that could conceivably throw an exception, although for this short example nothing clever is done about an exception.

The fundamental algorithm of the applet can be written as follows:

1.
Obtain the name of media file to play (using the parameter/property tag).

2.
Convert filename to a MediaLocator object.

3.
Create a Player object for the MediaLocator.

4.
Listen to the Player object for events.

5.
Start the Player object.

6.
Wait for the Player object to become realized.

7.
At the time of realization:

  • Obtain the Player object's visual component and place at top of applet.

  • Obtain the Player object's control component and place at bottom of applet.

Steps 1–5 all occur within the init() method, which is called when the applet is initialized. Step 6 is an expression of the event-driven nature of the program: There is no loop waiting or checking for realization to occur. Rather, it will be signaled by an event generated by the player. Step 7 occurs within the controllerUpdate() method, which is called when the Player object generates an event. If that event is a RealizeCompleteEvent, the Player object's components are obtained and added to the applet.

Two particulars of Listing 7.4 are worth further explanation. First, the initial line inside the try block of the init() method chains several steps together that result in a fully qualified name for the media object to play. As a first step in that process the value of the applet property that specifies the name of the file is obtained. That is combined with the document base of the applet, typically the same directory in which the applet is found, to produce a URL (object) that is then transformed back to a String (suitable for the MediaLocator constructor). Second, a layout manager—BorderLayout—is employed to ensure reasonable positioning of the controls and display on the screen. This allows the visual component of the player to be added to the top of the applet, whereas the controls are added to the bottom.

It is worth noting that a more complete example would likely override such methods as start() and stop() in order to control or free resources (that is, the Player object) as appropriate. Further, detecting and responding to the other types of events generated by the Player object could be used to provide additional functionality (for instance, looping play).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset