Hi
Im trying to write a frontend for mplayer in java, but there are some things that seem to be completely impossible to solve, so im hoping to get some advice. Another note: i am using linux.
Controling mplayer is no problem so far, but its really hard to embed it in a good way. I have used an quite ugly hack from jjmplayer to gain the window id for a Canvas. That way i can give mplayer the parameter -wid and define the window to output the video on. Some code:
There is a lot more code here, but the post got too long.
public void mplayerstart(Canvas overlay, String filePath)
throws ... {
...
if (osName.indexOf("Windows") >= 0) {
final Class<?> cl = Class.forName("sun.awt.windows.WComponentPeer");
java.lang.reflect.Field f = cl.getDeclaredField("hwnd");
f.setAccessible(true);
winid = f.getLong(overlay.getPeer());
}
if (osName.indexOf("Linux") >= 0) {
final Class<?> cl = Class.forName("sun.awt.X11ComponentPeer");
java.lang.reflect.Method m = cl.getMethod("getContentWindow", null);
Object obj = m.invoke(overlay.getPeer());
winid = Long.parseLong(obj.toString());
}
System.out.println(winid);
try {
...
parameters = parameters + " -wid " winid;
mplayerProcess = Runtime.getRunti ....
...
}
This code works and i also have an componentlistener to fix the aspect ratio of the video when the canvas gets resized. The problem is: entering fullscreen mode is almost impossible. Giving mplayer the fullscreen parameter wont do anything, so i have tried something like this:
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice device = env.getDefaultScreenDevice();
if (device.getFullScreenWindow() == window) {
window.removeNotify();
window.setUndecorated(false);
window.setVisible(true);
device.setFullScreenWindow(null);
} else {
window.removeNotify();
window.setUndecorated(true);
window.setVisible(true);
device.setFullScreenWindow(window);
}
In order to get into exclusive fullscreen mode i need to set the JFrame undecorated, and to do so i need to call removeNotyfy(). That is fine, but when i do that and make the frame visible again the window id of the canvas changes, and mplayer gives a lot of errors and no video. What i also have tried is creating a JWindow and adding the canvas to that one, but simply moving the canvas to another top level component changes its window id. Actually, the thing with a JWindow for fullscreen is a solution i like because i can add different components easily.
I have gotten close to a solution with two different approaches:
1. Restart mplayer every time i do enter fullscreen to giva a new window id. This works, but is WAY to slow, especially with streams. Also, i have not found a way to change window id in mplayer during playback, which would make this a good solution.
2. An additional ugly hack. I have created a JWindow just for mplayer and made it always stay on top while the main window with the controls is activated. I used an dummy JPanel to set the wanted position of the video output and CompoentListener to resize, move and fix the aspect ratio of the JWindow with the mplayer Canvas. This does work, but menus get behind the JWindow and, well, the behaviour is really ugly.
So, can anyone give some tips about where to look for a solution? I have some solutions in mind, but i dont know how to use any of them:
* Easiest one: (but not java related) Change the window id in mplayer during playback.
* Move the Canvas to another window
without changing the window id. Note: to get the window id for the canvas i use this hack:
Canvas overlay = someCanvas:
long winid = 0;
if (osName.indexOf("Windows") >= 0) {
final Class<?> cl = Class.forName("sun.awt.windows.WComponentPeer");
java.lang.reflect.Field f = cl.getDeclaredField("hwnd");
f.setAccessible(true);
winid = f.getLong(overlay.getPeer());
}
if (osName.indexOf("Linux") >= 0) {
final Class<?> cl = Class.forName("sun.awt.X11ComponentPeer");
java.lang.reflect.Method m = cl.getMethod("getContentWindow", null);
Object obj = m.invoke(overlay.getPeer());
winid = Long.parseLong(obj.toString());
}
System.out.println(winid);
Here are some references in case some one is interested:
[http://beradrian.wordpress.com/2008/01/30/jmplayer/]
[http://www.mplayerhq.hu/DOCS/tech/slave.txt]
[http://tivo-mplayer.sourceforge.net/docs/mplayer-man.html]
[http://jjmplayer.sourceforge.net/] (i got the hack from here)
I can provide more information if necessary, i had to remove more then half of my (attempt to) first post due to the limit here. Thanks in advice for help.
Edited by: vedder20 on Feb 21, 2009 11:17 AM
Edited by: vedder20 on Feb 21, 2009 12:21 PM