Home > Software > JMPlayer – Embedding MPlayer in Java

JMPlayer – Embedding MPlayer in Java


MPlayer is a very good (if not the best) open source multimedia player. But what has got to do with Java? A lot of people will argue with me and they will jump immediately to say JMF. Java Media Framework, in my opinion, is an obsolete, unfriendly, unmaintained library. So let me tell you, although I’m a Java fan, if you want multimedia playing capabilities in your Java apps, until a good revival initiative for JMF is there on the market, MPlayer is the answer.

If anyway, we are not using Java native library, why MPlayer? Here the answer is simpler, even tough still debatable. I’ve chosen MPlayer for several reasons:

  • it is open source
  • it is well maintained and updated with the latest video formats
  • it is easy embeddable in your application using the slave mode and easy to integrate in your installer
  • it is ported for almost all the major OSes (Windows, Unix, Mac) so the portability won’t suffer too much

This is gonna be a little bit bigger article as I’ll walk you through all the details, especially the trickier ones). Before reading this you should have basic Java knowledge (especially threads and I/O). You may also want to read before or refer to MPlayer documentation and its slave mode (try this link if the previous one does not work).

First of all, the big picture: we will start, from our Java application, MPlayer in a separate process, in slave mode. MPlayer, in slave mode, accepts commands (terminated by newline character) from standard input, executes them and print the response, if any, to the standard output. So we will inject commands into the MPlayer standard input (that being an output stream for us) and we will parse the MPlayer standard output and standard error (input streams for us) to interpret the answers.

To start MPlayer use the below:

Process mplayerProcess = Runtime.getRuntime().exec("/path/to/mplayer -slave -quiet -idle file/to/play.avi");

This will start MPlayer in a separate process. /path/to/mplayer is the path to the mplayer executable. Then we have a few command line options:

slave
mandatory; tells MPlayer to start in slave mode
quiet
optional; reduces the amount of messages that MPlayer will output
idle
optional; it doesn’t close MPlayer after a file finished playing; this is quite useful as you don’t want to start a new process everytime you want to play a file, but rather loading the file into the existing already started process (for performance reasons)

Now we will redirect the standard output and error of MPlayer into other two (or, my preference, only one) other streams. This should be continuously and we will do it in separate threads. For the sake of simplicity I’ll describe first a helper class:

class LineRedirecter extends Thread {
    /** The input stream to read from. */
    private InputStream in;
    /** The output stream to write to. */
    private OutputStream out;
    
    /**
     * @param in the input stream to read from.
     * @param out the output stream to write to.
     * @param prefix the prefix used to prefix the lines when outputting to the logger.
     */
    LineRedirecter(InputStream in, OutputStream out) {
        this.in = in;
        this.out = out;
    }
    
    public void run()
    {
        try {
            // creates the decorating reader and writer
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            PrintStream printStream = new PrintStream(out);
            String line;
    
            // read line by line
            while ( (line = reader.readLine()) != null) {
                printStream.println(line);
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
    
}

Basically the LineRedirecter class is a thread that will read from one input stream, line by line, and it will write to an output stream. The both streams are specified in the constructor. If an error occurs or if the input stream is closed, the thread ends gracefully. If you have basic knowledge on streams and I/O this is actually very simple so I’ll insist no more here.

And now back to MPlayer

// create the piped streams where to redirect the standard output and error of MPlayer
// specify a bigger pipesize than the default of 1024
PipedInputStream  readFrom = new PipedInputStream(256*1024);
PipedOutputStream writeTo = new PipedOutputStream(readFrom);
BufferedReader mplayerOutErr = new BufferedReader(new InputStreamReader(readFrom));

// create the threads to redirect the standard output and error of MPlayer
new LineRedirecter(mplayerProcess.getInputStream(), writeTo).start();
new LineRedirecter(mplayerProcess.getErrorStream(), writeTo).start();

// the standard input of MPlayer
PrintStream mplayerIn = new PrintStream(mplayerProcess.getOutputStream());

readFrom and writeTo creates a piped input output stream used by the above threads to redirect the standard output and error of MPlayer into a single stream. It is recommended to use a bigger buffer for the piped stream than the default one (in my example I use 256kb).

From now on we will use mplayerIn to send commands to MPlayer and mplayerOutErr to read and parse the answers. Now you’re practically set, but to help you I’ll just give you some sample code to see how everything is working

Open a new file
mplayerIn.print("loadfile \"/path/to/new file.avi\" 0");
mplayerIn.print("\n");
mplayerIn.flush();

Notice here two things. First: the path to the new file is enclosed in " as the file name contains space characters. Second: I haven’t used println, but print and then printed a newline character. println in some OSes (e.g. Windows) also prints the \r character and MPlayer will interpret it as part of the command, leading to an error.

Pause/play the current file
mplayerIn.print("pause");
mplayerIn.print("\n");
mplayerIn.flush();
Get the total playing time of the current file
>mplayerIn.print("get_property length");
mplayerIn.print("\n");
mplayerIn.flush();
String answer;
int totalTime = -1;
try {
    while ((answer = mplayerOutErr.readLine()) != null) {
        if (answer.startsWith("ANS_length=")) {
            totalTime = Integer.parseInt(answer.substring("ANS_length=".length()));
            break;    
        }
    }
}
catch (IOException e) {
}
System.out.println(totalTime);

Of course, this can be refined. We can also look to see if there is no error output, we can try to match against a regular expression, rather than test the starting string.

Jump to a given time in file
mplayerIn.print("set_property time_pos 300");
mplayerIn.print("\n");
mplayerIn.flush();

Jumps at the beginning of minute 5 in the movie (300 represents the seconds from the start)

Close MPlayer
mplayerIn.print("pause");
mplayerIn.print("\n");
mplayerIn.flush();            
try {
    mplayerProcess.waitFor();
}
catch (InterruptedException e) {}

Beside the fact that it sends the quit command to MPlayer also waits until the MPlayer process previously created is finished.

Now that you got the hang of it, you can create pretty complex interfaces or automation tasks with MPlayer.

All the code samples were tested with MPlayer CCCP 1.0rc2-4.2.1.

Later edit: I compiled a small one for you and you can download it here.

About these ads
Categories: Software Tags: ,
  1. nico
    March 5, 2008 at 11:00 am | #1

    the command Runtime.getRuntime().exec()
    does not accept regular expressions like file*.avi.
    That is a bit disturbing for conversion tasks. Do you know a work around ?

  2. March 6, 2008 at 9:53 am | #2

    Not sure if I understood exactly what you need.
    It supports this only if these are parameters passed to an executable and if that executable supports it.
    If you’re referring to opening a file then you should use java.awt.Desktop#open().

  3. Trevor
    March 8, 2008 at 11:38 pm | #3

    Do you have a demo application that I can download ? Thanks!!

  4. March 18, 2008 at 2:21 pm | #4

    I compiled a small one for you and you can download it from http://beradrian.users.sourceforge.net/articles/JMPlayer.java.
    You can modify it to fit your needs.

  5. Radu
    May 24, 2008 at 4:46 pm | #7

    Hello!

    I tried the solution posted here and it’s working wonderful! It allowed me to embed mplayer in my RCP application. One thing i didn’t manage to do: after the video started playing, how can i change the video size when a certain event occurs, for example when i resize the application window? I’ve seen that mplayer in slave mode has “set_property width new_width” and “set_property height new_height”…but mplayer returns an error when i send it those commands :|. Anyone has any ideas regarding this problem?

    Thanks in advance!

  6. October 1, 2008 at 1:00 pm | #8

    Thanks for the info, finally a kind fellow Java programmer care to share how to embed mplayer. By the way, i have linked to this post from my blog. Do you mind if i archive your code in my blog?

  7. October 1, 2008 at 1:59 pm | #9

    The code is free to share and modify, its solely purpose being to help you in not losing time with this (as I did). So yes, you can put it on your blog.

  8. October 12, 2008 at 11:40 am | #10

    Hello,

    Thanks for the examples but I have a question about the deployment of such application, how can I embed the mplayer in the installer of my application?
    (the installer should be platform independent too) or it may be a OS specific installer (i.e. .exe, .msi, .deb, .rpm, .bin or whatever mac os x uses)
    Thanks in advance

  9. pbp
    October 13, 2008 at 8:04 am | #11

    Thanks for your code, it work great except the slave mode. e.g. when mplayer window come up, it get focus and still accept input from keyboard. I run it on ubuntu 8.04, Any ideas?

  10. October 13, 2008 at 8:36 am | #12

    Have you tried starting MPlayer with the option −noconsolecontrols?

  11. October 13, 2008 at 8:40 am | #13

    Regarding the installer: usually an installer is platform specific. For Windows I personally use NSIS.
    To embed MPlayer simply take the MPlayer distribution and pack it into your installer. When you start MPlayer, the path will be relative to your installation directory.

  12. October 13, 2008 at 9:57 am | #14

    Hello again thanks for your replay,
    when you say “simply take the MPlayer distribution and pack it into your installer” do you mean a dll file or what??as when you go to the MPlayer website there is just too many builds…

    anyway thank you for this great blog :)

  13. pbp
    October 13, 2008 at 10:12 am | #15

    no luck. mplayer still accept input from keyboard. i can press ‘f’ for toggle ‘fullscreen’ mode. :(

  14. October 13, 2008 at 10:19 am | #16

    I meant the entire MPlayer distribution – executable, libraries, config file; practically everything you need to run MPlayer.
    Simply download the binary package for your OS and unpack it in a subfolder mplayer on your installation folder.

  15. October 13, 2008 at 10:27 am | #17

    To inhibit keyboard input, try the option -input conf=[filename] and specify a blank file. This should practically define no key bind to MPlayer commands.

  16. October 13, 2008 at 3:40 pm | #18

    Hello,

    An not sure that I got what you said cause when I downloaded the mplayer package for windows all I found is a .exe files can you please point me to the package you are talking about.

    Thanks again

  17. October 13, 2008 at 9:45 pm | #19

    Let’s take for example http://www.mplayerhq.hu/MPlayer/releases/win32/MPlayer-mingw32-1.0rc2.zip. Everything what’s in the zip file under the MPlayer-1.0rc2 folder should go in your installation package.

  18. tian
    December 7, 2008 at 12:13 pm | #20

    Hi,
    could you tell me why the command “get_property length” return error. I’ve tried “get_time_length” too with the same result. I’m testing it on .AVI file or .VBO. Everything else work great. I will be glad for help.

  19. December 7, 2008 at 9:45 pm | #21

    For me “get_property length” works perfectly on “.avi”. Maybe you can post or send me some code.

  20. quark
    January 30, 2009 at 7:49 am | #22

    Hello
    Is it possible to change the value of loop
    during the playback which is running in slave mode.???
    I started the process like:
    mplayerProcess = Runtime.getRuntime().exec(“mplayer -slave -quiet -idle /home/videos/chacko.wmv -loop 100″);
    I want to reduce the loop value to 50. Is it possible??

    Thanks.

  21. January 30, 2009 at 1:06 pm | #23

    Yes. Just use loop [value] or set_property loop [value].
    And in Java:

    mplayerIn.print("set_property loop 50");
    mplayerIn.print("\n");
    mplayerIn.flush();

  22. Quark
    February 1, 2009 at 4:22 am | #24

    Thanks. But that will add value 50 to the existing loop for 100 times. So that the result will be loop 150. I solved this by using a – to subtract a value from the existing loop value. Like,

    mplayerIn.print(“loop -50″);
    mplayerIn.print(“\n”);
    mplayerIn.flush();

    Thank you.

  23. Quark
    February 1, 2009 at 8:27 am | #25

    How can i check the video is currently playing or not. Actually i want to execute some code after the playback finished. Like,

    mplayerIn.print(”loop -50″);
    mplayerIn.print(”\n”);
    mplayerIn.flush();

    //wait for playback completing. then execute next statements,
    …………….

    Thanks

  24. February 1, 2009 at 1:50 pm | #26

    Unfortunately, I don’t think there is a way to do this.
    A workaround will be to start mplayer again for every playback. And to detect when it is finished you can see my other article

  25. Abdullah
    March 2, 2009 at 8:19 am | #27

    http://www.geocities.com/beradrian/storage/JMPlayer.java

    is really a good piece of code.

    Thanks

  26. Adam
    March 17, 2009 at 4:34 pm | #28

    This is great info. I’m new to mplayer and I’d like to extract the frames of a video and have the frame images piped to stdout so that I can capture them into some kind of Collection from w/in the java program. Any idea how to do this?

    Thanks

  27. papo
    March 20, 2009 at 5:21 pm | #29

    Hello

    Thank you for sharing this. I have a small suggestion: When you try to use a command which generates some output you want to read, mplayerOutErr.readLine()blocks for quite some time. You need to flush the printStream in the while loop of the LineRedirecter class after writing to it to speed up things.

  28. March 24, 2009 at 11:03 am | #30

    For Adam: I don’t know actually how this can be done, but have you tried the −dumpvideo option in MPlayer?

  29. Piti
    March 29, 2009 at 11:57 am | #31

    It’s great article, thx a lot.
    To @papo: thanks I thought about this problem and I did’n know what’s wrong, you gave me an answer :)

  30. KM
    June 28, 2009 at 9:40 am | #32

    Hi!
    Really gr8 article, it helped me to know about MPlayer quickly.

    When I play the playlist using -playlist option, ther is delay and between the each video play. Can’t we reduce it to play all files sequentially?

    And also the player window is getting minimised after every video completion.

    And I want to play the video on FullScreen mode.

    Can anybody help in this regard.
    Any help would be appriciable.

    Thanks
    KM

  31. June 29, 2009 at 3:52 pm | #33

    You can play entire playlists instead of files.

    For fullscreen you can try:
    mplayerIn.print(“set_property fullscreen 1″);
    mplayerIn.print(“\n”);
    mplayerIn.flush();

    • KM
      July 3, 2009 at 3:25 am | #34

      Thanks Adrian for the replies!

      Again, I still see delay between each video present in the playlist. Can we reduce the delay?

      When I updated-added new videos to- the playlist in filesystem, MPlayer is not able to reload the lastest chages.

      Can not we get the MPlayer loading playlist dynamically with new updates with out stopping the player and reloading it?

      • July 3, 2009 at 10:21 am | #35

        I guess that the delay is due to the fact that MPlayer is loading the video. I don’t know if you can do something like preload.
        You can load list dinamically. Please see the command “loadlist ” from slave mode documentation.

  32. zzzin
    July 9, 2009 at 1:11 pm | #36

    Can I put Mplayer in a new Jframe??

    • July 13, 2009 at 12:28 pm | #37

      As far as I know, this is not possible. At least in easy way. Maybe using JNI and making your code totally platform dependent.

  33. Jim
    July 29, 2009 at 8:12 am | #38

    Thank’s for this tutorial.
    My project involves only sound and url instead of files. Your sample code works great to open first url.
    But how to (click a button or enter in menu as SMPlayer) and open another url? loadfile doesn’t seem to work, mplayer quit! What may be the code for an empty play function and send url’s as string to open?
    Thank’s again.

    • July 29, 2009 at 8:45 am | #39

      loadfile should work. I think you should escape the backslash (simply double it). Try also with append = 1.

      • Jim
        November 1, 2009 at 1:41 pm | #40

        Adrian, sorry this took a long time.
        Appending 1 doesn’t work. Still mplayer quits.
        What exactly do you mean by escape backslash?. there is the code:
        s.print(“loadfile http://94.232.114.240:6804 0\n”);

  34. October 2, 2009 at 1:37 pm | #42

    Great article. Thanks for posting this.

    One question: Has anyone been successful in embedding the player in the associated java application? Right now, I’ve got everything working, but the MPLayer always opens in its own window. I’d like MPlayer to open up in a Frame within the java app. Is that possible?

    Thanks again for the posting.

    • October 2, 2009 at 9:16 pm | #43

      Unfortunately I don’t know a (simple) way to do this. For sure you will need to write JNI code for doing it.

      • October 8, 2009 at 5:13 pm | #44

        While reviewing the protocol documents, I noticed that under the Slave Protocol, there is something called “change_rectangle”. There are also properties for “height” and “width”. But as best I can tell, these don’t seem to do anything. Or I am sending them wrong.

        Any ideas on what these commands/properties do and/or how to use them?

        There is an option to set the location and size of the video window when the application starts, but I can’t seem to find a way to move or adjust its size once the window is open.

        Just curious if anyone has tried these commands and/or if they even work.

        Thanks.

  35. applesingh
    February 23, 2010 at 6:09 pm | #45

    can you use mplayer as a backend to java but without popping a separate mplayer window. I mean the video should remain lets say within jmframe etc.
    let me know if any of you has some sample code demonstrating this.

    Thanks in advance

    • February 24, 2010 at 10:09 pm | #46

      Please see the comments above.
      As far as I know, this is not possible. At least in an easy way. Maybe using JNI and making your code totally platform dependent.

  36. Alain
    March 29, 2010 at 4:15 pm | #47

    Hi Adrian!!I’m doing a project involving mplayer and i would like to know if there is a list of commands available in java to controll basic things such as volume, record, brigthness,color…

    Thanks!!

    • March 29, 2010 at 4:20 pm | #48

      Please read this post. All the commands are the ones available in slave mode and are sent from Java as described above.

  37. Marco
    April 23, 2010 at 4:43 pm | #49

    Hi Adrian,
    tnx for your great work

    I’m using MPlayer embeded in a java swing window, using option -wid to redirect mplayer output.
    This work fine when i start a process, but don’t work when I try to “loadfile”.
    In this case a new player is open without using my existent java window

    did you have any suggestion/ideas ?

    PS: I know this is out of scope of this site but I think it can be interesting with for mplayer-java embedding ..

    tnx

    • April 23, 2010 at 6:58 pm | #50

      Have you tried with “loadfile file 1″ and then “pt_step”?
      Can you post your code here so can other benefit too?

      Thanks,
      Adrian.

    • April 1, 2014 at 7:11 am | #51

      Can you post your code, I would like to see how you embedded video using swing

  38. video converter
    May 8, 2010 at 7:32 pm | #52

    Great informative post!

  39. Martin
    May 9, 2010 at 8:28 pm | #53

    Hey!
    Great piece of code! I just implemented it into my app, works fine. Does anyone know how I can read mp3 or wma tags without starting a playback? I tried several things, but none of them worked..

    Thanks in advance,
    Martin

  40. John
    May 10, 2010 at 9:44 am | #55

    Hi Adrian,

    Lot of thanks for this great information.

    In my scenario, I want to have my custom playlist in my application that keeps changing dynamically. Could you please tell me how I could pass one by one to the MPlayer?

    Thanks,
    John

    • May 10, 2010 at 12:50 pm | #56

      Please read the full documentation about the slave mode and its commands. In this case the command is ‘loadfile’.

  41. John
    May 10, 2010 at 9:56 am | #57

    Dear Adrian,

    I have another question. Could you please tell me how I could know that the current file has been played fully? Any flag or property that I need to keep checking?

    Thanks,
    John

    • May 10, 2010 at 12:49 pm | #58

      Please read the full documentation about the slave mode and its commands. In this case the commands are ‘get_time_pos’ or ‘get_percent_pos’.

  42. John
    May 10, 2010 at 12:52 pm | #59

    Hi Adrian,

    Thanks for your reply.

    Thanks,
    John

  43. John
    May 10, 2010 at 4:03 pm | #60

    Hi Adrian,

    Could you please tell me how I could wait for the completion of the playing of a song? I see in the code that we could wait for Starting Playback…

    Thanks,
    John

  44. video converter
    May 11, 2010 at 5:16 pm | #62

    You gathered the useful information people would find interesting here.

  45. Jacobus Reyneke
    May 25, 2010 at 3:12 pm | #63

    Hi Adrian,

    You have maintained this thread with great care for 2 years and 5 months so far! Impressive man!!!!

    Keep it up!
    Jacobus

  46. Jacobus Reyneke
    May 28, 2010 at 10:35 am | #64

    Tip:
    If someone needs to play one file at a time, but have a problem with the mplayer flickering between videos:
    Just change this line
    private String mplayerOptions = “-slave -idle”;

    to this
    private String mplayerOptions = “-fixed-vo -slave -idle”;

    and for full screen playback, just add -fs. I know these parameters are in the mplayer docs, but if you are like me, you would have missed them somehow ;-)

    Question:
    Adrian, I need to display text and logos over the currently playing file. If you have done something like this, would you please share it with a snippet of code or a bit of advice?

    Kind regards,
    Jacobus

    • May 28, 2010 at 12:56 pm | #65

      I haven’t done this before. But have you tried creating a skin? Or a filter?

      • Jacobus Reyneke
        May 31, 2010 at 11:08 am | #66

        I’ll look into filters…. I hope it’s easy ;-)

  47. Phil
    July 13, 2010 at 9:03 am | #67

    Is there a way to skip through frames for audio files? Say for an mp3 file, I want to play from frame 10000 to the frame size. Or is there a way to seek through audio files? Seeking only seems to work for video.

    • Phil
      July 13, 2010 at 5:57 pm | #68

      Nevermind, found it.
      seek [type]
      Seek to some place in the movie.
      0 is a relative seek of +/- seconds (default).
      1 is a seek to % in the movie.
      2 is a seek to an absolute position of seconds.

      so in code,

      jmPlayer.execute(“seek 20″); or jmPlayer.execute(“seek 20 0″); will seek 20 seconds + the time already played,
      jmPlayer.execute(“seek 20 1″); will seek to 20% of the entire media length, and
      jmPlayer.execute(“seek 20 2″); will seek to 20 seconds of the media.

      Hope this helps anyone else that might have stumbled into this.

  48. mo
    July 21, 2010 at 5:12 pm | #70

    Hi,

    Great piece of work.

    I am having a slight problem.

    I start mplayer with file(“x.avi”) //loadfile(“x.avi”) plays perfectly.

    However during playback of x.avi, i want to play another file instead so

    for testing purposes
    (in java code i have a delay of 30 seconds then)

    I want to then play another file (“y.avi”) //loadfile(“y.avi”)

    Mplayer slave process starts the platback of y.avi but then pauses after like 20 secs.

    This happens for any/every file I play.

    Is there a solution to this.

    I would be greateful for your comments.

    Thanks,

    • mo
      July 22, 2010 at 12:15 pm | #71

      Ok – i found an issue.

      If i load file y – then it pauses after.
      If i load file y twice – it then plays correctly but it seems to be played in its own independent process.

      To correct this problem – I have start another mplayer process and it works fine.

      (the problem before I was starting two processes from the same thread and it didn’t work correctly however starting another jmplayer thread worked ok for me.

      Cheerz

  49. lazerous
    July 23, 2010 at 4:09 pm | #73

    The is an excellent example. Is it possible to use this to embed onto a Canvas embedded in an already existing JFrame?

    I have seen mixed success on this across the web, and great many folks telling me it can only be done using windows.

    In my own attempts I have only managed to to either hear the audio and see no video, or have the video open in its own windows.

    • July 23, 2010 at 7:13 pm | #74

      Please see the comments above. To use MPlayer embedded in a java swing window, using option -wid to redirect MPlayer output.

  50. Mansy
    July 30, 2010 at 4:41 am | #75

    Hi Adrian,

    Great thread, really, many thanks!

    I was trying to use mplayer to read from a named pipe (in Windows). Basically, I want to write a Java program that is going to write a video stream to a named pipe (e.g. \\.\pipe\pipename) and have the mplayer play the stream from the pipe. Making a pipe is trivial in Linux, but I haven’t found anything about making a pipe in Windows from Java. Any ideas?!

    Generally, is there a better way to achieve what I’m trying to do “using mplayer to play a video stream coming from a Java program not from a file”?! Thanks

    • July 30, 2010 at 11:30 am | #76

      I don’t know how to do it with a named pipe (if you find out please post your solution here). If acceptable, I’ll do a server in Java; mplayer can play URLs too.

      • Mansy
        July 31, 2010 at 4:08 am | #77

        A server is fine, I’m just trying to do it in a better way (at least I think this is better). I read in the documentation that you can stream a movie to the stdin of mplayer using “mplayer -stdin”. I have been trying to do that, but I still can’t make it work!! Here is the code I used:

        String cmd = “mplayer -slave -cache 65536 -stdin”;
        try {
        Process mplayerProcess = Runtime.getRuntime().exec(cmd);

        FileInputStream fis = new FileInputStream(new File(“video.avi”));
        BufferedInputStream bis = new BufferedInputStream(fis);

        BufferedOutputStream bos = new BufferedOutputStream(mplayerProcess.getOutputStream());

        int c=0;
        while( (c = bis.read()) != -1) {
        bos.write(c);
        bos.flush();
        }
        bis.close();
        bos.close();
        }catch(Exception e) {
        e.printStackTrace();
        }

        when I execute this I get the following exception:

        java.io.IOException: The pipe is being closed
        at java.io.FileOutputStream.writeBytes(Native Method)
        at java.io.FileOutputStream.write(Unknown Source)
        at java.io.BufferedOutputStream.flushBuffer(Unknown Source)
        at java.io.BufferedOutputStream.flush(Unknown Source)
        at java.io.BufferedOutputStream.flush(Unknown Source)
        at streaming.StreamToMplayer.main(StreamToMplayer.java:45)

        Any ideas?!

  51. Macware
    October 8, 2010 at 7:49 pm | #78

    Thanks for the instruction!
    I’ve learned a lot. Many thanks!
    But I just wonder how I can set the size and the position of mplayer?
    Are there several commands available.

    • October 11, 2010 at 9:26 am | #79

      Have you tried with width and height?
      Or the command line options -geometry, -wid, -guiwid?

  52. bidou
    November 11, 2010 at 6:51 am | #80

    Hi there,

    This is a inquiry for the webmaster/admin here at beradrian.wordpress.com.

    Can I use some of the information from this blog post above if I give a backlink back to your site?

    Thanks,
    Daniel

  53. Fox
    June 13, 2011 at 7:03 pm | #82

    Hi Adrian,

    Is there a way to ask the state of mplayer (paused or running)?

    Thank you in advance!

    • June 14, 2011 at 1:18 am | #83

      You should try get_time_pos and get_time_length. See slave mode for more info.

  54. Fox
    June 15, 2011 at 5:14 pm | #84

    Hi Adrian,

    And thanks for your answer. I have a little other question for you:

    I noted that when is used the command “pausing_keep” and “get_time_pos”, the video if is paused goes in play mode for a little while. Is there a “trick” to keep it in pause mode?

    Thank you again!

  55. Fox
    June 16, 2011 at 4:03 pm | #85

    Alternatively…. I need to disable the keyboard in the Video window (Specially SPACE-BAR for play/pause mode) . I tested this command but it don’t work:

    >mplayer.exe” -idle -slave -input conf=C:\temp\input.conf “C:\temp\video.avi”

    where the file input.conf is an empty file; but mplayer doesn’t recognize the option.

    Sorry for my multiple questions. I hope you can help me.

  56. Anonymous
    June 25, 2011 at 3:34 am | #86

    Hi Adrian,
    I tried your code JMPlayer.java on Windows but it does not work… the reason, at least in my case (but I suspect it probably depends on the mplayer version/build) is that the output messages of mplayer start with the source mplayer module name,
    so your mehod waitForAnswer will never find the expected message and blocks inside the loop…

    first solution is to change startsWith() in contains() but after some research in mplayer man page I found the option “-nomsgmodule” that if added to the command line will suppress module name in the mplayer responses making your sample work.
    To me, the second solution seems more robust..

    What do you think, am I missing something here…?

    Thanks

  57. priyanka
    December 8, 2011 at 1:27 pm | #87

    Hi Adrian,
    I am tring to display live video streaming with multicast IPs using SDP(Session Description Potocol) in a linux environment using mplayer. Where at a time 4 mplayer instances are running. But the problem is, while playing some recorded video its working fine. But for Live streaming, the last call to mplayer with the IP address is displaying at all four instances. Means the same video is displayed at all the 4 places. Do u have any idea???

    One more question, I am tring this from a java call to mplayer but i need to display a AWT component or window (anything) above the mplayer. How to achive that??

  58. Al Sargent
    April 13, 2012 at 6:42 am | #88

    Adrian,
    I would like to use your compile-able code to play just audio files. I call the various functions from another class. It plays the files fine but when I try to stop the file from playing the the mplayerProcess in the execute method is always null so the command does not execute. I am obviously missing something along the way. Thank You!

  59. Al Sargent
    May 28, 2012 at 6:31 pm | #89

    Adrian,
    Does an audio file have to be playing before the volume can be changed?
    Thanks.

  60. October 27, 2012 at 1:54 am | #90

    Thank you for the auspicious writeup. It in fact was a amusement account it.
    Look advanced to far added agreeable from you! By the way,
    how could we communicate?

  61. April 9, 2013 at 5:58 am | #91

    Hi Adrian, can you help me?

    Everytime I call mplayerIn.flush(), MPlayer window ALWAYS close with message “Exiting… (End of file)”. I tried calling different commands (getPlayingFile(), getTimePosition(), getTotalTime(), getVolume()) but still no luck.

    However, I can successfuly calling those commands from the command line (get_property path, get_property time_pos, get_property length, get_property volume).

    My system: Ubuntu 12.10 64-bit, MPlayer2 UNKNOWN (C) 2000-2012 MPlayer Team, Java 1.7.0_17.

    Please help.

    -Rahmadi D-

    • April 10, 2013 at 9:44 am | #92

      Hi,

      I already found the cause. Before, I call MPlayer using this syntax:
      mplayerProcess = Runtime.getRuntime().exec(new String[] { mplayerPath, videoPath, “-slave -idle -quiet” });

      It turned out that ALL parameters must be separated, otherwise MPlayer will wrongly detect them. So I change it to this:
      mplayerProcess = Runtime.getRuntime().exec(new String[] { mplayerPath, videoPath, “-slave”, “-idle”, “-quiet” });

      And now MPlayer runs fine :)

  62. keysuke
    May 30, 2013 at 7:22 am | #93

    Hi,
    i’m linux user and i tried to open a file that have spaces in name (track with spaces.mp3), but i got errors, i noticed that Mplayer was trying to open each string before the space.

    Tried to put in linux format (track\ with\ space.mp3), but doesn’t work too. Any tips?

  63. aristogeit
    February 3, 2014 at 7:48 am | #94

    ” obsolete, unfriendly, unmaintained library”

    does that mean unuseable? no

    so stop repeating this nonsense

  64. Anonymous
    March 25, 2014 at 7:08 am | #95

    Amazing artile, it really shows how to use workarounds and stop complaining about the incapabilities. mplayer is a great commandline media player.

    However, now I believe it is time to start using the inbuilt capabilities of Java through JavaFX libraries. They are being actively development by Oracle and support a lot of file formats flawlessly, and in real sense ensure portability across platforms without need to install any new libraries other than Java (since JavaFX now is by default installed with Java) :)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: