MattHicks.com

Programming on the Edge

Java Media Components (JMC) in Swing

Published by Matt Hicks under , , , , , on Sunday, May 03, 2009
If you're like me and have been really excited by all the hype of JMC, but think of yourself as a Java developer and not a fly being beckoned to the newest scripting framework with its bright and illuminating glow (JavaFX that is) then JMC comes as something of a disappointment. After all, every article on the web referencing JMC seems to inseparably link JavaFX and JMC as if they absolutely *MUST* go together.

However, this is not the case. I've blogged before about the evils of JavaFX and how Java developers can leverage the functionality without selling your soul to the scripting demons:

http://www.matthicks.com/2009/02/i-hate-javafx-i-love-javafx.html

Someone else has taken this another step by taking the same approach for JMC:

http://nishimotz.com/2009/03/10/using-jmc-from-java/

Now, though what Takuya wrote is fully functional, I hate the idea of having to include JavaFX just to use JMC. After a bit of digging I have come to find what seems to be a hidden feature (maybe Sun just thinks the idea of someone using it outside of JavaFX is ridiculous and not worth mentioning?), the jmc.jar file you get with JavaFX has some interesting classes in it that you can take advantage of in your own Swing applications without any hackery at all:


package test;

import java.io.File;

import javax.swing.JFrame;

import com.sun.media.jmc.JMediaPlayer;

public class TestJMC {
public static void main(String[] args) throws Exception {
JFrame frame = new JFrame("Testing JMC"); {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 600);

JMediaPlayer player = new JMediaPlayer(new File("trailer_1080p.mov").toURI()); {
frame.add(player);
}

frame.setVisible(true);
}
}
}


I hate it when people give references like this and don't give any mention of a test file that can be used along with it, so to let you know what I used, I simply grabbed the trailer for "Big Buck Bunny" here:

http://www.bigbuckbunny.org/index.php/trailer-page/

What's awesome is that any one of them should work fine. Now, the player controls kind of suck, but in that same package there are other, probably more useful Swing classes: JMediaPane and JMediaView.

One last note. You need to include the jmc.dll (or the correct native lib for your OS) file in your library path to be able to play or you'll get unfriendly errors.

Have fun being able to leverage full video playing in your Swing applications.