A Simple Way of Embedding a Video in My Swing Gui

A simple way of embedding a video in my Swing GUI

I dont know why you think you need a lot of code to use JMF.

public class MediaPlayer extends JFrame {
public MediaPlayer() {
setLayout(new BorderLayout());

//file you want to play
URL mediaURL = //Whatever
//create the media player with the media url
Player mediaPlayer = Manager.createRealizedPlayer(mediaURL);
//get components for video and playback controls
Component video = mediaPlayer.getVisualComponent();
Component controls = mediaPlayer.getControlPanelComponent();
add(video,BorderLayout.CENTER);
add(controls,BorderLayout.SOUTH);
}
}

A complete media player in like 6 lines, prob could have done it in less. If all you need is something basic, then id go with JMF.

As Zemzela mentioned, Xuggle is also a good solution but will require more work.

There are also Java bindings VLC. Click Here

How to embed a 10 second MP4 video in java GUI?

I don't know if/how it is possible in pure Swing, but you can embed a JavaFX MediaPlayer in your Swing application, see details here: https://stackoverflow.com/a/16326393/1149528 or here: Can't play mp4 converted file - JavaFX 2.1

Embedding a video in my Java GUI


..am I wrong about this?

Yes, you are. The only methods that are automatically included in a frame are those defined for it.

The methods you are talking about, are part of the JMF. Those classes need to be imported at the top of the code. Something like:

import javax.media.*;

How To Play MP4 Video In Java Swing App

Thanks to @VGR for bringing JavaFX to my attention, I just integrated a JFXPanel into a JPanel of where I wanted the video to be. It's working perfectly fine in my case since it's a simple screen with one video to play.

Here's the full code snippet below:

private void getVideo(){
final JFXPanel VFXPanel = new JFXPanel();

File video_source = new File("tutorial.mp4");
Media m = new Media(video_source.toURI().toString());
MediaPlayer player = new MediaPlayer(m);
MediaView viewer = new MediaView(player);

StackPane root = new StackPane();
Scene scene = new Scene(root);

// center video position
javafx.geometry.Rectangle2D screen = Screen.getPrimary().getVisualBounds();
viewer.setX((screen.getWidth() - videoPanel.getWidth()) / 2);
viewer.setY((screen.getHeight() - videoPanel.getHeight()) / 2);

// resize video based on screen size
DoubleProperty width = viewer.fitWidthProperty();
DoubleProperty height = viewer.fitHeightProperty();
width.bind(Bindings.selectDouble(viewer.sceneProperty(), "width"));
height.bind(Bindings.selectDouble(viewer.sceneProperty(), "height"));
viewer.setPreserveRatio(true);

// add video to stackpane
root.getChildren().add(viewer);

VFXPanel.setScene(scene);
//player.play();
videoPanel.setLayout(new BorderLayout());
videoPanel.add(VFXPanel, BorderLayout.CENTER);
}

Once the getVideo() function is made, I called it in the constructor of the JFrame to trigger it on the applications launch.

Ways to make my own video play back?

JMF seems to have been around since 1997. No wonder then that, as you said, most of the Java-based players out there rely on it.

So I decided to take a trip back in time using Google's search options and found a result from Feb 2001: a very simple GNU-licensed MPEG-1 player implemented from the ground up using just Java and C. How about that? The following capture is from the website:

MPEG-1 player

I know you emphasised you needed a solution with as little external code as possible. In this case, no additional libraries seem to be required but you have to do some compiling. Plus you're limited to MPEG-1. So, not exactly what you were looking for but perhaps worth a look.

Hope it helps!

Embed a YouTube video to JFrame?

Here's the way I usualy use to embed YouTube videos into Swing application.

Instead of YouTube API a native browser component is embedded into JPanel using DJ Native Swing:

public class YouTubeViewer {

public static void main(String[] args) {
NativeInterface.open();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame("YouTube Viewer");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.getContentPane().add(getBrowserPanel(), BorderLayout.CENTER);
frame.setSize(800, 600);
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
});
NativeInterface.runEventPump();
// don't forget to properly close native components
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
@Override
public void run() {
NativeInterface.close();
}
}));
}

public static JPanel getBrowserPanel() {
JPanel webBrowserPanel = new JPanel(new BorderLayout());
JWebBrowser webBrowser = new JWebBrowser();
webBrowserPanel.add(webBrowser, BorderLayout.CENTER);
webBrowser.setBarsVisible(false);
webBrowser.navigate("https://www.youtube.com/v/b-Cr0EWwaTk?fs=1");
return webBrowserPanel;
}
}

When running it looks like

Sample Image

The following libraries are necessary to launch an example above:

  • DJNativeSwing.jar
  • DJNativeSwing-SWT.jar
  • swt-4.3-win32-win32-x86.jar (This one is platform dependent)

you can get all of them from a DJ Native Swing download package.

Embedding a browser in Swing in the hopes of using a VLC applet

If you're only trying to play video then there are other alternatives to video playback then trying to get a Browser to display properly in Java. Video and Browser's are the two hardest things to do in Swing still and there's no sense trying to tackle both if you only need one.

However, if you do need both then once you have the Browser working you can use Flash pretty easily at that point for a lot of video and audio playback.

I've used two separate solutions for embedding a web browser into a Swing application. The first is pretty simple, you can wrap a SWTBrowser into a java Swing Component. The only downside here is that you get whatever the native browser is on each platform. The second is there is a Swing XULRunner project out there. XULRunner is what Firefox is built upon. It's a bit hard to find the project but it lets you fully customize the browser and use the same one on each platform.

What is the best way to turn this Java Swing application into a sandboxed embed-able (applet or Web Start)?

Redo the client using HTML5 and Javascript so that you don't run into any prompts or security warnings.



Related Topics



Leave a reply



Submit