how to control VLC by java

17,626

Solution 1

You are probably looking for VLCJ, this is a java wrapper for VLC. It allows you to embed VLC media player in a java application, and thus add all your personal controls.

(Do note that for user applications this is fine, but the VLCJ library isn't perfect, you can have several problems pop up.)

EDIT: For my project I've seen memory leaks and issues with long running programs (multiple instances for several hours). Especially the multiple instances doesn't work in combination with some compile options (which are on by default).

Solution 2

VLCj is what you're after yes - it's essentially a straight Java wrapper around libvlc. If you use it in process (especially if you use multiple players in process) you will sometimes see VM crashes - this isn't VLCJ's fault, rather libvlc and the native libraries it uses underneath have some subtle threading bugs that exposes these problems.

You can get it working reliably with multiple instances, but to do so you need to use it out of process. See here for my initial attempts at doing so. It's a bit of work to set up but once going, things seem to work very nicely.

Solution 3

Depending on what you want to ultimately accomplish maybe the gstreamer Java bindings are worth a look which will give you a very fine grained control about the playback. And you can do conversions and everything. They also have a minimalistic video player example application to get you started with.

Share:
17,626
sajad
Author by

sajad

Updated on June 17, 2022

Comments

  • sajad
    sajad about 2 years

    I want to run a program called VLC in java and control it while running, for example if user clicked on ❚❚ or ►► button, I do a specific suitable action.

    I run VLC by this code :

    try
    {
        Runtime rt = Runtime.getRuntime();
        Process p = rt.exec(VLCProgramAddFile + " udp://@:" + listeningPort);
    
        OutputStream out = p.getOutputStream();
        InputStream in = p.getInputStream();
    
        p.waitFor();
        System.out.println("End of VLC");
    }
    catch (Exception e)
    {
        System.out.println("error in running VLC");
    }
    

    I have heard about Java bindings, but I don't know how does it work for this job.

  • sajad
    sajad almost 14 years
    Thank you, I am trying to learn how to use VLCJ but is there any way to control this called process through in/out streams in my program? Using VLCJ is better or calling .exe file, like I used in my program.Please notice that controlling video while running, is important to me.
  • Thirler
    Thirler almost 14 years
    @Sajad I've only got experience with using VLCJ, it is at least possible to start/stop and pause from the application, but I think all commands will work. I have no experience with using the normal VLC gui in combination with your own application. I think if you want to embed the output into your application you need VLCJ.
  • sajad
    sajad almost 14 years
    Thank you for your worthy answer.
  • Admin
    Admin almost 13 years
    Do not use Gstreamer. I did that at the end after years i realized how difficult it is for Crossplatform. IF you are in linux its fine.
  • Admin
    Admin almost 13 years
    VLC is the best in crossplatform compared to all others.
  • Felix
    Felix about 11 years
    I've been experiencing these sporadic JVM crashes running VLCj in process. Do these problems disappear when VLCj is run out of process? I'm about to try to make VLCj run out of process, but don't want to go to the effort if the end result is also unreliable.
  • Michael Berry
    Michael Berry about 11 years
    @nissemand If you run one VLCJ instance in process and it's fine, but multiple instances sporadically crash, then yes - this should make these problems disappear. If just one instance crashes, then the problem likely lies elsewhere.
  • Felix
    Felix about 11 years
    I see. I've only been running one instance at a time, but many sequentially. I'll try move them out of process and see what happens. Thanks for the help berry120!
  • John
    John over 8 years
    Did you achieve it with multiple instances using out-of-process implementation ?
  • Michael Berry
    Michael Berry over 8 years
    @John Yes, though it is rather fiddly.
  • John
    John over 8 years
    @berry120 could you please share your solution ?
  • Michael Berry
    Michael Berry over 8 years
    @John I've described it on my blog here if you want to take a look: berry120.blogspot.co.uk/2011/07/…
  • Victor
    Victor over 5 years
    @Thirler I know is a long time, but... I am also experiencing troubles with long time running videos with VLCJ. Do you have any tips & tricks that you used in order to handle them. What tweaks should I take into consideration ?
  • Thirler
    Thirler over 5 years
    @Victor Unfortunately I don't have the code we used at the time anymore (nor do I remember).
  • Victor
    Victor over 5 years
    @Thirler Thanks anyway ! Due the lack of helpful documentation on VLCJ (or maybe I searched the wrong places) I have came up with another "solution" - I am opening the VLCJ player in another process and when it crashes (after some days), I am detecting it and restart the process - still work in progress, but I do not find any possible roadblocks here. Is not quite a solution because the error is still occurring.