Java Media Framework basics
Presented by developerWorks, your source for great tutorials
ibm.com/developerWorks
Table of Contents
If you're viewing this document online, you can click any of the topics below to link directly to that section.
1. About this tutorial....................................................... 2
2. A simple audio player.................................................. 5
3. JMF user interface components ..................................... 9
4. Conceptual JMF ........................................................ 13
5. Broadcasting and receiving media .................................. 16
6. Wrapup and resources ................................................ 22
Java Media Framework basics Page 1 of 25
Section 1. About this tutorial
What is this tutorial about?
The Java Media Framework (JMF) is an exciting and versatile API that allows Java
developers to process media in many different ways. This tutorial provides an overview of
some of the major features of JMF, mainly through the use of working examples. Upon
completion of this tutorial, you will understand the major players in the JMF architecture. You
also will have worked directly with JMF, using live examples and source code that may be
extended for more specific purposes.
Topics covered in this tutorial are as follows:
• Tips for downloading and installing JMF
• Major JMF classes and their uses in the JMF architecture
• Playing local media files
• Presenting a graphical user interface (GUI) for media access and manipulation
• Broadcasting media over a network
• Receiving broadcast media over a network
Almost any type of media manipulation or processing is possible through JMF. A
comprehensive discussion of all the features JMF has to offer is well beyond the scope of
this tutorial. Instead, we'll use three simplified media applications to learn about the
framework's building blocks. In this way, the tutorial will prepare you for future study and the
implementation of more specific applications.
Should I take this tutorial?
This tutorial will walk you through the basics of working with JMF. To accomplish this, we'll
create three separate working example applications. Each of these examples will build upon
the previous examples, displaying different aspects of JMF's functionality.
The examples in this tutorial assume that you have used and are already familiar with the
Java programming language. In addition to the core Java and JMF classes, we will be
working with some of the Java AWT and Swing classes (for presenting a GUI), as well as
some Java networking classes (for transmitting media over a network). Some familiarity with
both the GUI and networking classes will help you more quickly understand the discussion
and examples here, but are not prerequisites of this tutorial.
The example applications we'll work with are as follows:
• A simple audio media player (the JMF HelloWorld application): This
command-line-driven media player lets you play most audio types by simply specifying the
media file name on the command line. The audio media player demo showcases mainly
JMF-specific classes.
• A GUI-driven media player: We'll use built-in JMF interface components to build the GUI,
so little GUI programming experience will be necessary for this exercise. The media viewer
demo uses some of the Java AWT and Swing classes to present GUI components to the
ibm.com/developerWorks
Presented by developerWorks, your source for great tutorials
Page 2 of 25 Java Media Framework basics
user.
• A media broadcasting application: This application allows a local media file to be
transmitted over a network. The application is flexible enough to transmit media only to a
specified network node, or to broadcast it to all the nodes within a subnet. This demo uses
some of the Java networking APIs to allow media transmission over a network.
As part of the third exercise, we'll modify the GUI-enabled media player to let it receive and
play broadcast media.
See Resources on page 23 for a listing of articles, tutorials, and other references that will help
you learn more about the topics covered in this tutorial.
Installation requirements
To run the examples in this tutorial, you need the following tools and components:
• The Java 2 platform, Standard Edition, to compile and run the demo applications
• The Java Media Framework, version 2.1.1a or above
• A properly installed and configured sound card
• One or more test machines
• The demo source files in mediaplayer.jar
The final demo application shows how JMF can be used over a network. If necessary, the
demo can be run on a single machine using the machine as both the transmitter and
receiver. To see the full power of using JMF over a network, however, you will need at least
two machines that are reachable on the same network.
See Resources on page 23 to download the Java 2 platform, complete source files, and other
tools essential for the completion of this tutorial.
Downloading the installer
The first step in installing the JMF on your machine is to download the installer through the
JMF home page, which also contains links to the JMF source code and the API
documentation. See Resources on page 23 for a link to download JMF.
Currently, JMF has versions for Windows, Solaris, Linux, and a pure-Java version that works
on any machine with a JVM. To increase performance, you should download the version that
is specific to your OS. Any code that is written and compiled with an OS-specific version of
JMF is portable across other OSs. For example, if you download the Solaris version of JMF
and compile a number of classes, those same classes can be used on a Linux machine
without any problems.
Alternatively, you may choose to download the pure-Java, or "cross-platform," version of
JMF. This version doesn't employ any OS-specific libraries. The cross-platform version is a
good choice if your OS doesn't have its own version of JMF installed or if you don't know
what OS the target machine will be running.
Presented by developerWorks, your source for great tutorials
ibm.com/developerWorks
Java Media Framework basics Page 3 of 25
Installing JMF
After you've downloaded the JMF installer to your desktop, double-click the installer icon.
Most installers have an option to install native libraries in a system directory; for example, the
Windows installer will present an option to "Move DLLs to Windows/System directory." For
best results, choose this option, because it ensures the OS-specific libraries will be installed
correctly.
You should also choose the option to update the system CLASSPATH and PATH variables
during installation. If this option is turned off, remember to include the JMF jar files in the
classpath when you compile or run any of the examples in this tutorial.
About the author
Eric Olson is a software developer at Retek Inc. He has four years of experience with the
Java platform, and is experienced with many different Java-based technologies, including
JMF, Jini, Jiro, JSP, servlets, and EJBs. Eric graduated from the University of St. Thomas in
St. Paul, MN, with a degree in Computer Science. He has worked for IBM on the
SanFrancisco Project and the WebSphere Business Components initiative. He has also
worked for Imation Corp. on storage appliances. He currently is developing Web-based
software solutions for the retail industry. In his spare time, Eric collaborates with Paul
Monday on Stereo Beacon -- a distributed peer-to-peer media player based on JMF. Contact
Eric at zpalffy@yahoo.com.
ibm.com/developerWorks
Presented by developerWorks, your source for great tutorials
Page 4 of 25 Java Media Framework basics
Section 2. A simple audio player
Overview
In this section, we'll walk through the first exercise of creating a simple audio player. This
example introduces you to the Manager class and the Player interface, which are two of
the major pieces in building most any JMF-based application.
The functional goal of this example is to play a local audio file through a command-line
interface. We'll walk through the source code and review what is happening in each line.
After completing this section, you will have a demo application on which you can play any
audio file type supported by JMF, including MP3, WAV, and AU, among many others.
Refer to the SimpleAudioPlayer.java file in the source code distribution to follow this
exercise.
Importing the necessary classes
The first few lines of the SimpleAudioPlayer class include the following calls, which
import all necessary classes:
import javax.media.*;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.MalformedURLException;
The javax.media package is one of the many packages defined by JMF. javax.media is
the core package, containing the definitions of the Manager class and the Player interface,
among others. We'll focus on the Manager class and the Player interface in this section,
and deal with some of the other javax.media classes in later sections.
In addition to the import javax.media statement, the above code fragment includes
several import statements that create the input to our media player.
The Player interface
In the next code fragment, the public class SimpleAudioPlayer and the Player instance
variable are defined:
public class SimpleAudioPlayer {
private Player audioPlayer = null;
The term Player may sound quite familiar, because it is based on our common use of
audio- and video-based media players. In fact, instances of this interface act much like their
real-life counterparts. Players expose methods that relate to the functions of a physical
media player such as a stereo system or VCR. For example, a JMF media Player has the
Presented by developerWorks, your source for great tutorials
ibm.com/developerWorks
Java Media Framework basics Page 5 of 25