System News
Playing Audio and Video With the Java Media Framework API
Java Developer Connection Program Tech Tips
February 25, 2002,
Volume 48, Issue 4

The Java Developer ConnectionSM Program (JDC) Tech Tips for February 19, 2002, written by John Zukowski covers:

  • Playing Audio and Video With the JavaTM Media Framework API
  • Using the JSObject Class in Applets

These tips were developed using JavaTM 2 Software Development Kit (SDK), Standard Edition, v 1.3.

Playing Audio and Video With the Java Media Framework API

You can use the core Java 2 Platform libraries to display still images in GIF, JPEG and PNG format. The libraries also provide limited support for displaying animation through the GIF89A image format. You can also play WAV, AU, MIDI and AIFF-formatted audio files. This support might be sufficient for your programs, but if you need to work with other rich media formats, such as AVI files for video or MP3 files for audio, you need the Java Media Framework (JMF) API.

The JMF API supports the playing, streaming and capturing of audio and video. It provides a series of encoders and decoders to support different formats and offers a pluggable architecture for adding support for additional formats.

Playing multimedia files through the JMF libraries is simple. The key classes are Manager and Player. The resource you pass into the createPlayer method can be one of three things: DataSource, MediaLocator or URL. These represent different ways of specifying media: as a protocol handler (DataSource), through its content (MediaLocator) or by its location (URL). In most cases, working with the URL resources is simplest.

This process is not sufficient to play video. The Player is a type of Controller, and controllers let you register a ControllerListener. An event subtype that is particularly important is RealizeCompleteEvent. When this event happens, the ControllerAdapter delegates the handling to the realizeComplete method. Overriding this method permits you to get the visual component for the video player and the control panel component for audio and video playing. The control panel is where you can control audio volume and start or stop video.

There is an example online.

Using the JSObject Class in Applets

Applets are Java technology programs that run in the browser. Under most circumstances, an applet does its processing within the realm of the JavaTM runtime environment (JRE). There are times, however, when it is necessary to jump out of the applet's secure runtime environment (the "sandbox") and communicate with the browser. The communication is done through the LiveConnect facility and permits applets to work with JavaScriptTM technology. The bulk of this communication is performed through the netscape.javascript.JSObject class.

Originally provided as part of the Netscape browser environment, Microsoft eventually added this package to the runtime that ships with Internet Explorer. In addition, the package is now a standard part of the JRE installed with the JavaTM Plug-in.

In order to develop applets that take advantage of this package, you need to add the necessary classes to your class path. The JRE already puts these classes in your runtime class path by default. However the development environment doesn't. So you need to make available to your development environment the jaws.jar file (Windows) or javaplugin.jar file (SolarisTM Operating Edition ((Solaris OE)) that ships with JRE.

Even without Java Plug-in installed, you can still use the netscape.javascript.JSObject class with the native virtual machine in Internet Explorer 4.x - 6.x and Netscape Communicator 4.7x. The JavaScript communications support in a native virtual machine differs from that provided with the Java Plug-in. The basics described in this tip are the same. However, the native virtual machines rely on an older version of the package.

There is code and additional technical details available online:

Read More ... [ more...]



Other articles in the Java Technology section of Volume 48, Issue 4:

See all archived articles in the Java Technology section.

Trending in
Vol 234, Issue 3
Trending IT Articles