Приглашаем посетить
Дружинин (druzhinin.lit-info.ru)

4.5 Using the Java Media Framework (JMF) and Player

Table of Contents

Previous Next

4.5 Using the Java Media Framework (JMF) and Player

4.5.1 Installing and using JMF

In addition to the Java package J2SDK, JMF is an optional software package from Sun Microsystems dedicated to multimedia applications. The package enables audio, video, and other time-based media to be added to Java applications and applets. JMF is a powerful package containing a large number of functions such as playback, capture and stream video, or movie broadcasting.

In addition to Java, this package greatly enhances the multimedia capability of browsers across different platforms. JMF is a powerful toolkit for Web developers to design scalable multimedia applications. Basically, there are three versions of JMF:

  • jmf-2.1_1a-alljava.zip A cross-platform JMF

  • jmf-2.1_1a-win.exe The JMF Performance Pack for Windows

  • jmf-2.1_1a-solaris.bin The JMF Performance Pack for Solaris SPARC

All three packages can be downloaded from the official site of Sun Microsystems (java.sun.com or java.sun.com/products/java-media/jmf/index.html).

The cross-platform version was written entirely using the Java programming language and is designed for use on Java-capable machines. The Performance Pack for Windows is a JMF version optimized for the Windows systems. The Solaris Performance Pack is designed to run on and is optimized for the Solaris platform.

If you are using Windows, it is recommended that you download and install the Windows version. The executable program jmf-2.1_1a-win.exe is an "InstallShield" executable. That means that by double clicking on this program, JMF will be installed easily. Also, the program detects whether or not a default browser is installed and then installs JMF for its use.

If you are installing the generic version jmf-2.1_1a.zip, you may need to run the appropriate zip command such as winzip or unzip to extract JMF onto your system and directory. The JMF of this version was written using Java bytecode ready for Java-enabled machines.

If you install JMF yourself, you may need to set up the system variable (or environment variable) so that the JMF classes are available through the CLASSPATH variable. For example, if you are using a UNIX/LINUX machine, you may need to set up the environment using the following command:



setenv JMFHOME /home/someuser/JMF2.1.1
setenv CLASSPATH $JMFHOME/lib/jmf.jar:.:${CLASSPATH}

For Windows systems, you may need to set the following:



set JMFHOME=C:\JMF2.1.1
set CLASSPATH=%JMFHOME%\lib\jmf.jar;.;%CLASSPATH%

If you are using Windows, the "set" command inside a DOS window should display all environment variables on your machine. You should examine the CLASSPATH settings to make sure that JMF is set up correctly. Alternatively, the official site of Sun Microsystems (www.java.com) contains a utility or a page to test the JMF installation and configuration for your machine.

Bundled with JMF is a program called Java Media Studio (JMStudio). At a minimum level, this program can be used as a media player to play back a large number of sound, music, and movie files. It is also a good tool to convert movie formats from one form to another.

For example, you can follow the instructions below to convert an avi video to QuickTime movie mov format.

Step 1. Activate the JMStudio as in Fig. 4.35.

Figure 4.35. The JMStudio program

graphics/04fig38.gif


Step 2. Go to the menu File | Open File and load the avi file, e.g., carxx1.avi, onto the studio (Fig. 4.36).

Figure 4.36. Open the AVI file

graphics/04fig39.gif


Step 3. Go to the menu File | Export to activate the "Export" window (Fig. 4.37). From the format field, select the QuickTime (mov) format. Press the Save button.

Figure 4.37. The "Export" window

graphics/04fig40.gif


Step 4. From the "Save As" window as in Fig. 4.38, type in the QuickTime movie name (e.g., carxx2.mov).

Figure 4.38. Save the QuickTime movie carxx2.mov

graphics/04fig41.gif


That's it. The QuickTime movie should be saved inside the directory that you have chosen. To see the QuickTime movie carxx2.mov, you can double click on the file and activate the QuickTime player for the playback. A screenshot of the playback is shown in Fig. 4.39.

Figure 4.39. Playback the QuickTime movie

graphics/04fig42.gif


This JMStudio program is free and is an example from the JMF package. The source code of this program is called JMStudio.java and can be compiled and launched using the following commands:



javac JMStudio.java
java JMStudio

As we mentioned in the previous chapter, the command javac in the first line is used to compile a Java program. The result in this case is a program JMStudio.class. The second command java is used to launch the application (see Fig. 4.35).

Now you have some experience of using JMF and its associated player. JMF is a powerful tool for handling multimedia applications. It would be interesting if we were to embed the player into a Web page and use it for the Web community.

4.5.2 Play back sound and movies using JMF

We mentioned earlier that the JMF package was written by the Java language. Whether we use it as a standalone programming language or as a language on the Web, Java is a powerful tool for developing professional applications. Even in a modest form, Java supports various multimedia formats. Consider the following Java program:



Example: SimpleSound.java - The Java Program To Play A Simple Audio Clip

 1: import java.applet.*;
 2: import java.awt.*;
 3: import javax.swing.*;
 4:
 5: public class SimpleSound extends JApplet
 6: {
 7:    private AudioClip sound;
 8:    public void init()
 9:    {
10:       sound = getAudioClip( getDocumentBase(), "dvd02.wav" );
11:       sound.play();
12:    }
13: }

This is a very simple Java program to play back sound files. The program will generate a class bytecode which can be used on a Web page. The javax.swing package in line 3 contains an object called AudioClip. This object is used to define a variable sound in line 7. The main part of this program contains two statements only:



sound = getAudioClip( getDocumentBase(), "dvd02.wav" );
sound.play();

The first statement is to get the sound file dvd02.wav and assign it to the variable sound. Since the sound variable or object was generated by the object AudioClip, it contains all the properties and methods of AudioClip. A simple call to sound.play() would play back the sound file. One beauty of this example is that it will play back all sound files recognized by the Java language. You don't need to know the details and structure of the file itself.

To call this program, the following Web page is used:



Example: ex04-17.htm - Playback Simple Sound File Using Java Applet

 1: <?xml version="1.0" encoding="UTF-8"?>
 2: <!DOCTYPE html
 3:      PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 4:      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 5: <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 6: <head><title> ex04-17.htm </title></head>
 7: <body>
 8:  <applet code="SimpleSound.class" width="350" height="30">
 9:  </applet>
10: </body>
11: </html>

This page uses a simple applet element <applet> to call the program SimpleSound.class and ultimately play back the sound file.

The Java program SimpleSound.java is a sloppy example and far from a professional standard. At a minimum level, a professional multimedia application should contain the following features:

  • Play back sound file formats such as wav, mid, and au.

  • Play back movie file formats such as avi, mov, and mpg.

  • Contain standard controlling buttons such as Play, Stop, Forward, Backward, and a progress bar.

  • Contain error detection code to handle some basic errors.

Fortunately, we don't need to develop a program of a multimedia player like this. The JMF package contains a large number of media player examples to help us. For example, the Java program SimplePlayerApplet.java from JMF is a functional media player available for immediate use. To use this program, you need to compile it using



javac SimplePlayerApplet.java

The result is a class ready to be used as an applet from a Web page. Consider the following page:



Example: ex04-18.htm - Embedding The JMF Player

 1: <?xml version="1.0" encoding="UTF-8"?>
 2: <!DOCTYPE html
 3:      PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 4:      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 5: <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 6: <head><title> ex04-18.htm </title></head>
 7: <style>
 8:  .txtSt {font-family:arial;font-size:18pt;font-weight:bold}
 9:  .butSt{font-family:arial;font-size:12pt;color:#008800;
10:  background-color:#ffccccc;width:160px;height:26px;font-weight:bold}
11: </style>
12: <body style="background:#000088;color:#ffff00;text-align:center">
13: <div class="txtSt"> <br />
14:  Using The Java Media Player Applet <br />
15:  SimplePlayerApplet <br /><br />
16:
17: <applet code="SimplePlayerApplet.class"
18:    width="320"
19:    height="260"
20: >
21: <param name="file" value="dropxx1.avi" />
22: </applet>
23:
24: </div>
25: </body>
26: </html>

Also, this is a simple page to use the applet element to embed the SimplePlayerApplet.class in a Web page. The applet element <applet> in line 17 defines an area of dimension 320 x 260 for the player. The media file in this case is dropxx1.avi and is defined by the parameter element <param>. The Java program SimplePlayerApplet.java will get the file name from the <param> element and perform the multimedia playback. A screen shot of this example is shown in Fig. 4.40.

Figure 4.40. Embedding JMF Player (ex04-18.htm)

graphics/04fig43.gif


Putting the media file into the <param> element is a good move. In fact, you don't need to know the details of the Java program if you don't want to. Now, let's consider some examples to add more features to this media player without touching the Java code.

4.5.3 A page to select and play back movies

If you have a large number of movies, how would you deliver them on the Internet? One of the simplest and most effective methods is to display each movie as one button. When the user clicks one of the buttons, the corresponding movie will be played by the player. To develop this example, you can use the Java program SimplePlayerApplet.java in section 4.5.2 as a player.

The idea for this example is simple. Basically, one button for each movie is generated. When one of the buttons is clicked, the following statements similar to lines 1722 of example ex04-18.htm are generated:



<applet code="SimplePlayerApplet.class"
   width="320"
   height="260"
>
<param name="file" value="dropxx1.avi" />
</applet>

If you substitute the media file dropxx1.avi in the <param> element with the movie of your choice, this applet code will activate the player to play back the movie you want.

Consider the page below:



Example: ex04-19.htm A Page To Select and Play Back Movies

 1: <?xml version="1.0" encoding="UTF-8"?>
 2: <!DOCTYPE html
 3:      PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 4:      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 5: <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 6: <head><title> ex04-19.htm </title></head>
 7: <style>
 8:  .txtSt {font-size:18pt;font-weight:bold;color:#ffff00;
 9:     background:#000088;font-family:arial}
10:  .butSt{font-family:arial;font-size:12pt;color:#008800;
11:  background-color:#ffccccc;width:160px;height:26px;font-weight:bold}
12: </style>
13:
14: <script>
15:  function show(ss)
16:  {
17:   llst='<applet code=SimplePlayerApplet width="320" height="270" >' +
18:    '<param name=file value="' + ss + '" /></applet> <br />' + ss
19:   document.getElementById('myId').innerHTML = llst
20:  }
21: </script>
22:
23: <body class="txtSt">
24: <div align="center">
25:    A Page To Select Movie<br /><br />
26:
27: <form name="myForm">
28:   <input type="button" value="robotxx2.avi" class="butSt"
29:        onclick="show('robotxx2.avi')" />
30:   <input type="button" value="dropxx1.mov" class="butSt"
31:        onclick="show('dropxx1.mov')" />
32: <input type="button" value="carxx2.mpg" class="butSt"
33:        onclick="show('carxx2.mpg')" />
34: </form>
35:
36: <div id="myId"></div>
37:
38: </body>
39: </html>

In this example, we want to play back three movies. They are robotxx2.avi, dropxx1.mov, and carxx2.mpg. Each movie is associated with one button defined in lines 28, 30, and 32. Consider the first button:



<input type="button" value="robotxx2.avi" class="butSt"
     onclick="show('robotxx2.avi')" />

This input element <input> with type="button" defines a button. This button has a display name called "robotxx2.avi" and a CSS style definition butSt. When this button is clicked, the function



show("robotxx2.avi")

is activated to play back the movie robotxx2.avi. The detail of this function is defined in lines 1520:



function show(ss)
{
 llst='<applet code=SimplePlayerApplet width="320" height="270" >' +
  '<param name=file value="' + ss + '" /></applet> <br />' + ss
 document.getElementById('myId').innerHTML = llst
}

First, the movie file name robotxx2.avi is substituted into this function as variable ss. Inside this function, there is a string called llst. This string stores the details of the applet element <applet>. When the ss variable is used in the string llst, the entire string llst turns out to be:



<applet code="SimplePlayerApplet.class" width="320" height="270" >
<param name="file" value="robotxx2.avi" /></applet><br /> robotxx2.avi

This code will activate the SimplePlayerApplet player to play back the file robotxx2.avi. Also, the file name is displayed at the bottom at the same time. Here we have used the JavaScript or ECMAScript function to output the XHTML applet element <applet>. ECMAScript will be discussed further in Part II of this book. The statement in line 19



document.getElementById('myId').innerHTML = llst

outputs the XHTML string llst to the location identified by the attribute id="myId" (see line 36). Note that the innerHTML property is not a W3C recommendation. It can be used to send an XHTML string to a particular location and is a popular technique among the Web community. A screen shot of this example is shown in Fig. 4.41.

Figure 4.41. Select a movie

graphics/04fig44.gif


Another way to make your selection and play your favorite movie is to use the XHTML select box.

4.5.4 Select and play back movie with a select box

A select box is a small text box with a down arrow on the right hand side. When a user clicks on the down arrow, the box will be extended and contain all the available options. The user can then make a selection among the options. To generate a select box containing three movies, the following XHTML code is used:



<form name="myForm">
  <select name="myMovie" class="butSt">
     <option value="robotxx2.avi">robotxx2.avi</option>
     <option value="dropxx1.mov">dropxx1.mov</option>
     <option value="carxx2.mpg"> carxx2.mpg</option>
  </select>
</form>

The three movies, robotxx2.avi, dropxx1.mov, and carxx2.mpg, will appear as options in the box. One of the reasons we put the form element <form> outside of the select box is that once a selection is made, this selection can be accessed by the command



myForm.myMovie.value

With all these at hand, you can develop the Web page as follows:



Example: ex04-20.htm - Select and Play Back Movies With Select Box

 1: <?xml version="1.0" encoding="UTF-8"?>
 2: <!DOCTYPE html
 3:      PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 4:      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 5: <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 6: <head><title> ex04-20.htm </title></head>
 7: <style>
 8:  .txtSt {font-size:18pt;font-weight:bold;color:#ffff00;
 9:     background:#000088;font-family:arial}
10:  .butSt{font-family:arial;font-size:12pt;color:#008800;
11:  background-color:#ffccccc;width:160px;height:26px;font-weight:bold}
12: </style>
13: <script>
14: function show(ss)
15: {
16:  llst='<applet code=SimplePlayerApplet width="320" height="270" >' +
17:       '<param name=file value="' + ss + '" /></applet> <br />' + ss
18:  document.getElementById('myId').innerHTML = llst
19: }
20: </script>
21: <body class="txtSt">
22: <div align="center">
23:      Select and Playback Movie <br />
24:      With A Select Box<br /><br />
25:
26: <form name="myForm">
27:   <select name="myMovie" class="butSt">
28:      <option value="robotxx2.mpg">robotxx2.avi</option>
29:      <option value="dropxx1.mov">dropxx1.mov</option>
30:      <option value="carxx2.mpg">carxx2.mpg</option>
31:   </select>
32:   <input type="button" value="Go" class="butSt"
33:        onclick="show(myForm.myMovie.value)" />
34: </form>
35: <div id="myId"></div>
36: </body>
37: </html>

This page is a modification of example ex04-19.htm to accommodate a select box. It is defined in lines 2731. To trigger the selection, a button is generated in lines 3233. When this button is pressed, the selection value is stored in the property



myForm.myMovie.value

By putting this value into the function



show(myForm.myMovie.value)

the applet code defined in lines 1419 would play back the movie you have selected. A screen shot of this example at work is shown in Fig. 4.42.

Figure 4.42. Select a movie from a select box

graphics/04fig45.jpg


One interesting feature of JMF is the capability to deliver live video broadcasting, and this will be discussed next.

4.5.5 Broadcasting live video using JMF

Broadcasting live video means we want to send and receive video pictures in real time across a network or the Internet. One typical application of live video is video conferencing on the Web. Unlike a document, a picture, or a fixed length video, live video (or live stream) usually may not have a predefined duration. Downloading the entire stream before playing it, in many cases, would be impossible.

Traditional HTTP and FTP suites are based on the Transmission Control Protocol (TCP). It is a protocol designed for reliable data transmission. Among other things, when a packet is lost or corrupted, the protocol requests another transmission. This will guarantee reliable data transfer and maintain the integrity of documents, pictures, and files. Because of this and the overhead involved, TCP is not ideal for live media transmission.

Alternatively, the User Datagram Protocol (UDP) is widely used for stream media purposes. UDP is similar to TCP, is a transport protocol, and can be used for data transmission. The main difference is that UDP is not reliable. Packets or data are no longer guaranteed to reach their destination. In fact, there is no guarantee that packets or data will arrive in the order that they were transmitted. All these drawbacks are tolerable when we use it for video conferencing and transmission. Based on UDP, the Internet Engineering Task Force (IETF) has developed a protocol called the Real-Time Transport Protocol (RTP) which can be used for real-time data transmission.

JMF can use this RTP to transmit real-time data. Also, media players associated with JMF can receive the transmission via RTP.

As a simple example, let's consider a Java program provided by JMF. This program is called VideoTransmit.java and can be used to transmit live video data to a location. In order to transmit data in real time, usually three parameters are needed and they are the "Source Locator," "IP Address," and "Destination Port." The source locator is to identify the media content to send and can be any one of the following:

  • A media file such as file://c:/book/chap04a/robotxx2.mov.

  • A URL such as http://www.pwt-ex.com/dropxx2.mpg.

  • A capture datasource, either "vfw://0" or "sunvideo://0/1/jpeg."

The IP address should be the usual IP address of the computer receiving the transmission. For our system, the address is 169.254.101.152. The port number can be any free port available. In this case, port 8080 is used in our example.

If you have a live camera such as a Webcam attached to your system, you can use this VideoTransmit.java program to transmit the live image to a location identified by the IP address and port. First, you need to compile it to bytecode using the command:



javac VideoTransmit.java

You need to install JMF properly to do that. If the compilation is successful, you will have the program VideoTransmit.class on your system. To activate the program and start the transmission, you can use



java VideoTransmit vfw://0 169.254.101.152 8080

This command starts capturing the live image from the Webcam (or any capturing device) and transmits it to the destination 169.254.101.152 via the port 8080. By default, the transmission will last only 60 seconds. You can, however, change the Java program to suit your needs. A screen shot of this command running on a DOS window is shown in Fig. 4.43.

Figure 4.43. Transmitting live video using RTP

graphics/04fig46.gif


In order to receive the broadcast, you need to activate the media player and listen to the IP address and port number with RTP. For our simple example, consider the page below:



Example: ex04-21.htm - Receiving Live Video Broadcasting

 1: <?xml version="1.0" encoding="UTF-8"?>
 2: <!DOCTYPE html
 3:      PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 4:      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 5: <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 6: <head><title> ex04-21.htm </title></head>
 7: <style>
 8:  .txtSt {font-family:arial;font-size:18pt;font-weight:bold}
 9:  .butSt{font-family:arial;font-size:12pt;color:#008800;
10:  background-color:#ffccccc;width:160px;height:26px;font-weight:bold}
11: </style>
12: <body style="background:#000088;color:#ffff00;text-align:center">
13: <div class="txtSt"> <br />
14:   Receiving Live Video Broadcasting<br />
15:   Using RTP Protocol<br /><br />
16:
17: <applet code="SimplePlayerApplet.class"
18:    width="320"
19:    height="260"
20: >
21: <param name="file" value="rtp://169.254.101.152:8080/video" />
22: </applet>
23:
24: </div>
25: </body>
26: </html>

This is a simple page to activate one of the simple media players from JMF. The applet code activates the SimplePlayerApplet and plays back the media. The media content is specified by the parameter element <param> in line 21. Instead of providing the media file name to the program, we set up the RTP address for the value attribute. The rtp address used is

rtp://169.254.101.152:8080/video

Together with the IP address and port number, the content type is also used. A screen shot of the live transmission is shown in Fig. 4.44.

Figure 4.44. Receiving live video using RTP

graphics/04fig47.gif


More information on RTP, live media broadcasting, and samples is available on the JMF sections of the official site java.sun.com.

    Table of Contents

    Previous Next