Java Media Components

Java Media Components

The Java Media Components API promises to finally deliver an easy to use and easy to install platform for media playback in the Java Virtual Machine, and so far, they seem to have succeeded!
The other API that offers media playback on the JVM is the Java Media Framework: a library that requires the user to configure a registry containing information on available playback (and capture) devices and forces the developer to craft many lines of code just to play some video. Especially the configuration part for the user’s system makes the library in my opinion useless for inclusion in consumer applications.
The Java Media Components API is much easier and obtaining the library is the hardest part at this moment. There seems to be no public project, and the only access seems to be to follow these steps:

* Download the JavaFX preview
* Extract jmc.jar and jmc.dll from the zip: /javafx-sdk1.0pre1/lib/
* Include jmc.jar in your classpath
* Include -Djava.library.path= as a commandline parameter to your application.

If you fail to include the last step, the system will complain about not being able to find a codec for all media you’re trying to play. Currently this dependency on an external library means that the media components library is only usable on Windows and Mac. Hopefully the library will have evolved and ported when the final JavaFX release arrives.

The library is built around several usage patterns, that either offer an easier interface or give the developer more control. Let’s have a look.

The first option is the JMediaPlayer. This is a Swing component providing a the UI for both the video playback, as well as the controls for the user. THe following illustrates the use. Note that the actual component requires just one statement!

Code:
import com.sun.media.jmc.JMediaPlayer;
import java.net.URI;
import javax.swing.JComponent;
import javax.swing.JFrame;

public class BasicPlayer {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setTitle(“Basic Player”);
frame.getContentPane().add(createPlayer());
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

private static JComponent createPlayer() {
JMediaPlayer player = new JMediaPlayer(
URI.create(“file:///c:/demo/mad2a.wmv”));
return player;
}

}

The second pattern gives you more freedom, and doesn’t provide any visible controls: you’ll have to implement the interactions yourself. Since this would again require basically one line plus some code for a call to play, I spiced the application with a little bit of the SceneGraph API, inspired by this post of Kirill Grouchnikov.

Code:
import com.sun.media.jmc.JMediaPane;
import com.sun.scenario.effect.Reflection;
import com.sun.scenario.scenegraph.JSGPanel;
import com.sun.scenario.scenegraph.SGComponent;
import com.sun.scenario.scenegraph.SGEffect;
import com.sun.scenario.scenegraph.SGNode;
import com.sun.scenario.scenegraph.event.SGMouseAdapter;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.net.URI;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JComponent;
import javax.swing.JFrame;

public class ExtendedPlayer {
private JMediaPane mediaPanel;

public static void main(String[] args) {
new ExtendedPlayer();
}

public ExtendedPlayer() {
JFrame frame = new JFrame();
frame.setTitle(“Extended Player”);
frame.getContentPane().add(createScreen(), BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension(640, 640));
frame.setVisible(true);
}

private JComponent createScreen() {
JSGPanel panel = new JSGPanel();

SGComponent playerComponent = new SGComponent();
playerComponent.setComponent(createPlayer());
playerComponent.addMouseListener(new SGMouseAdapter(){
public void mouseClicked(MouseEvent arg0, SGNode arg1) {
mediaPanel.play();
}
});
SGEffect effect = new SGEffect();
effect.setEffect(new Reflection());
effect.setChild(playerComponent);

panel.setScene(effect);

return panel;
}

private JComponent createPlayer() {
mediaPanel = new JMediaPane(URI.create(“file:///c:/demo/mad2a.wmv”));
new Timer().schedule(new TimerTask(){
public void run() {
final double time = mediaPanel.getMediaTime();
System.out.printf(“%.1f\n”, time);
}

}, 1000, 100);
return mediaPanel;
}
}

The final way to use the library is doing without a Swing component. The MediaProvider class provides access to several so-called controls. These give you access to information on and control over audio, subtitles, tracks, video and rendering. This allows one to apply an effect to the video-frames before they are rendered or to render them into a 3D scene. The best way to learn about the controls is by looking at the methods provided by the different classes from within you favourite IDE.

Although currently the native codecs are the only ones available, thus limiting you in choice and complicating the delivery of media to all platforms, Sun is working on providing a cross-platform codec with JMC, which will be licensed from On2. Hopefully this will be ready when JavaFX is released in a few weeks. The JavaOne talk also mentioned that people are working on adding video capture to the API, allowing for video-conferencing. If they succeed we finally have a really nice API to work with media on the JVM!

2 thoughts on “Java Media Components

  1. I am Jeevan from MTech C.S.E. I need to play video using Swing Components.

    Example: When click button means in Swing pane itself it has to play video. With out opening new player..

    I am using netbeans IDE…
    Could you tell me how to implement this?..give me reply soon..its urgent..thanks for reading this comment…

Leave a comment