Home > Articles > Programming > Java

  • Print
  • + Share This
Like this article? We recommend

Like this article? We recommend

Play Media with a Basic Media Player

The jmc.jar archive organizes its many classes into several packages. You only need to work with the com.sun.media.jmc.JMediaPlayer class, and then only with one of this class's constructors, if you want to create a basic media player that provides its own control panel. Listing 1 presents an example of a basic media player that's based on JMediaPlayer.

Listing 1 BMP.java

// BMP (Basic Media Player).java

import java.awt.EventQueue;

import java.net.URI;

import javax.swing.JFrame;

import com.sun.media.jmc.JMediaPlayer;

public class BMP extends JFrame
  public BMP (String mediaURI)
   super ("BMP: "+mediaURI);
   setDefaultCloseOperation (EXIT_ON_CLOSE);

   JMediaPlayer mp = null;
     mp = new JMediaPlayer (new URI (mediaURI));
   catch (Exception e)
     System.out.println ("Error opening media: "+e.toString ());
     System.exit (1);

   setContentPane (mp);

   pack ();
   setVisible (true);

  public static void main (final String [] args)
   if (args.length != 1)
     System.err.println ("usage: java BMP mediaURI");

   Runnable r = new Runnable ()
            public void run ()
             new BMP (args [0]);
   EventQueue.invokeLater (r);

BMP.java uses the public JMediaPlayer(URI uri) constructor to create a media player that plays media content identified by uri. If the content is unavailable, this constructor throws a com.sun.media.jmc.MediaUnavailableException object.

If the uniform resource identifier's syntax is bad, the java.net.URI constructor throws a java.net.URISyntaxException object.

Assuming that the current directory contains BMP.java and that JavaFX 1.0 SDK has been installed to the default directory on a Windows platform, the following command line compiles this file's source code:

javac -cp "c:\Program Files\JavaFX\javafx-sdk1.0\lib\desktop\jmc.jar" BMP.java

The following command line (split across multiple lines for readability) runs the resulting basic media player application, which presents a media window and a control panel of media player controls below this window:

java -cp "c:\Program Files\JavaFX\javafx-sdk1.0\lib\desktop\jmc.jar";. 
     -Djava.library.path="c:\Program Files\JavaFX\javafx-sdk1.0\lib\desktop" 

Figure 1 shows a single frame of the Flash video that's being played.

Figure 01

Figure 1 Watching the JavaFX 1.0 launch video on the basic media player

The control panel presents various controls that, from left to right, let you restart the media, seek backward in the media, play/pause the media, seek forward in the media, seek to the end of the media, auto-repeat the media after it finishes playing, adjust the volume via the slider, and mute the audio.

I've found that seek-backward and seek-forward don't work properly, and seek-to-the-end isn't even implemented.

  • + Share This
  • 🔖 Save To Your Account