[MPlayer-cygwin] Using -wid to embed mplayer into a java SWT application.

Reinier Zwitserloot surial at gmail.com
Sun Sep 11 05:05:36 CEST 2005


I've noticed a previous thread on this earlier, but it doesn't look
like the poster of that question got anywhere. Unfortunately, I'm
stuck as well. The problem:

I'm trying to embed mplayer into a java application. SWT fortunately
makes it very easy to fetch the underlying windows handle so that I
can pass it on using the -wid parameter. I do this, and while the
sound starts playing no video shows. However, something sort of works,
because:

A) mplayer does start without complaint, so the hWnd I'm passing is
more or less valid, and
B) If I move the java window out of screen, mplayer starts giving me
'overlay errors'. Meaning mplayer does know something about my window
handle. Unfortunately if I drag the SWT shell over to my second
monitor the same errors happen, but that's a problem for another day.

The question then remains: Why isn't my video appearing?

Possibly my problem lies in the fact that I don't understand what
'colorkey' does, as it was mentioned as a neccessity for embedding
mplayer before. I checked out mplgui's source (it does what I want to
do, except it's delphi code), which uses the same colorkey I supplied
in this source.

Does the size of my window need to match the video output exactly for
this to work, or am I missing something perhaps?

Here's some code to serve as a starting point. I admit I hacked this
together rather quickly but it shows the gist. You also need to
download SWT (www.eclipse.org) if you want to compile this with javac.

(a decent chunk of the code is just starting up 2 threads to read the
error and output streams of the mplayer app and pass them through to
the console. Not the best way of doing it, but it works. The real
'meat' is the 'main' method at the bottom. Fix the 2 constants at the
top to point to mplayer.exe and any suitable video file.

Thanks in advance for any light on this problem!

 --Reinier Zwitserloot


compile with: javac -classpath (point to SWT's jar here) Player.java
run with:
java Player

if you get annoying 'missing DLL' errors, the quick and dirty way to
solve that is to copy swt.dll straight to c:\windows\system32. The
nicer way is to add it to the library path:
java -Djava.library.path=(path where SWT.dll resides) Player


//Source starts here.

import java.io.*;

import org.eclipse.swt.*;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.*;

public class Player
{
       //You'll have to edit these paths to something useful.
        private static final String MPLAYER =
"g:/util/video/mplayer/mplayer.exe";
        private static final String VIDEO_FILE =
"e:/dvd-backups/Firefly/Season 1/Firefly - s01e01 - The Train
Job.avi";

        public static Process setFile(final File pFile, final int hWnd)
          throws IOException
        {
                final String[] lCmds = new String[] {
                        MPLAYER,
                        "-wid", String.valueOf(hWnd),
                        "-nokeepaspect",
                        "-geometry", "0:0",
                        "-colorkey", "0x101010",
                        "-vo", "directx",
                        pFile.getAbsolutePath()
                };

                final Process lProcess = Runtime.getRuntime().exec(lCmds);
                final InputStream lOut1 = lProcess.getErrorStream();
                final InputStream lOut2 = lProcess.getInputStream();

                final Runnable lPrinter1 = new Runnable() {
                        public void run()
                        {
                                try {
                                        final BufferedReader lReader =
new BufferedReader(
                                                  new
InputStreamReader(lOut1, "UTF-8"));
                                                for ( String l =
lReader.readLine() ; l != null ; l =
lReader.readLine() ) {
                                                        System.out.println(l);
                                                }
                                } catch ( Throwable t ) {
                                        t.printStackTrace();
                                }
                        }
                };

                final Runnable lPrinter2 = new Runnable() {
                        public void run()
                        {
                                try {
                                        run2();
                                } catch ( Throwable t ) {
                                        t.printStackTrace();
                                }
                        }
                        public void run2()
                          throws Throwable
                        {
                                final BufferedReader lReader = new
BufferedReader(
                                  new InputStreamReader(lOut2, "UTF-8"));
                                for ( String l = lReader.readLine() ;
l != null ; l = lReader.readLine() ) {
                                        System.out.println(l);
                                }
                        }
                };

                new Thread(lPrinter1).start();
                new Thread(lPrinter2).start();

                return lProcess;
        }

        public static void main(String[] pArgs)
          throws Exception
        {
                final Display wDisplay = new Display();
                final Shell wShell = new Shell(wDisplay, SWT.SHELL_TRIM);
                final FillLayout wLayout = new FillLayout();
                wShell.setLayout(wLayout);
                final Control wVideo = new Canvas(wShell, SWT.EMBEDDED);

                wShell.setSize(800, 600);
                wShell.layout();

                wShell.setVisible(true);
                final Process lProcess = setFile(new File(VIDEO_FILE),
wVideo.handle);

                while ( !wShell.isDisposed() ) {
                        if ( !wDisplay.readAndDispatch() ) {
                                wDisplay.sleep();
                        }
                }

                lProcess.destroy();
        }
}




More information about the MPlayer-cygwin mailing list