import java.applet.*;
import java.awt.*;
import java.net.*;
import javax.media.*;
public class PlayerApplet extends Applet {
Player player = null;
public void init() {
setLayout( new BorderLayout() );
String mediaFile = getParameter( "FILE" );
try {
URL mediaURL = new URL( getDocumentBase(), mediaFile );
player = Manager.createRealizedPlayer( mediaURL );
if (player.getVisualComponent() != null)
add("Center", player.getVisualComponent());
if (player.getControlPanelComponent() != null)
add("South", player.getControlPanelComponent());
}
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();
}
}
In this case, we are using the static createRealizedPlayer() function of the Manager class to create the Player object. This ensures that the visual and control panel components are created before it gets added to the window by blocking until then. It is also possible to create an unrealized player and implement the ControllerEventHandler interface. The window then waits for the controllerUpdate event to fire and adds the components to the layout as they are realized:
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();
}
}
Using a simple applet tag, a multimedia stream can easily be embedded in a webpage:
<APPLET CODE=PlayerApplet WIDTH=320 HEIGHT=300>
<PARAM NAME=FILE VALUE="sparkle2.mpeg">
</APPLET>
This will create an applet with an embedded MPEG video stream: