how to control VLC by java
Asked Answered
W

3

6

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.

Waken answered 22/9, 2010 at 10:40 Comment(0)
E
5

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).

Exactitude answered 22/9, 2010 at 10:51 Comment(7)
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.Waken
@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.Exactitude
Thank you for your worthy answer.Waken
VLC is the best in crossplatform compared to all others.Sheepfold
@Exactitude 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 ?Ruhnke
@Ruhnke Unfortunately I don't have the code we used at the time anymore (nor do I remember).Exactitude
@Exactitude 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.Ruhnke
S
6

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.

Sundberg answered 5/8, 2011 at 22:12 Comment(7)
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.Jurywoman
@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.Sundberg
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!Jurywoman
Did you achieve it with multiple instances using out-of-process implementation ?Susi
@Susi Yes, though it is rather fiddly.Sundberg
@berry120 could you please share your solution ?Susi
@Susi I've described it on my blog here if you want to take a look: berry120.blogspot.co.uk/2011/07/…Sundberg
E
5

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).

Exactitude answered 22/9, 2010 at 10:51 Comment(7)
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.Waken
@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.Exactitude
Thank you for your worthy answer.Waken
VLC is the best in crossplatform compared to all others.Sheepfold
@Exactitude 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 ?Ruhnke
@Ruhnke Unfortunately I don't have the code we used at the time anymore (nor do I remember).Exactitude
@Exactitude 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.Ruhnke
C
1

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.

Concubinage answered 23/9, 2010 at 8:55 Comment(1)
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.Sheepfold

© 2022 - 2024 — McMap. All rights reserved.