gpt4 book ai didi

java - 在 Java 中启动 VLC 并通过 rc 接口(interface)连接到它

转载 作者:搜寻专家 更新时间:2023-10-31 20:07:32 27 4
gpt4 key购买 nike

我已经看过这个帖子,但我仍然遇到问题:starting vlc player in java看来 VLC 的 Java 绑定(bind)不再处于积极开发状态,并且无论如何也不支持命令行上的所有可能。

鉴于以下代码,我无法从 Mac OS 10.5.8 (Java 1.6) 上的 Java 应用程序启动 VLC,然后通过终端或其他 Java 应用程序的 rc 接口(interface)连接到它。

public class Main {

public static void main(String[] args) {
String s = null;


try {
//Process p = Runtime.getRuntime().exec("/Applications/VLC.app/Contents/MacOS/VLC -I telnet --telnet-host=localhost:4442 -I rc --rc-host=localhost:4444");
//Process p = Runtime.getRuntime().exec("/Applications/VLC.app/Contents/MacOS/VLC -I rc --rc-host=localhost:4444");

//ProcessBuilder pb = new ProcessBuilder("/Applications/VLC.app/Contents/MacOS/VLC","-I rc","--rc-host=localhost:4444");
ProcessBuilder pb = new ProcessBuilder("/Applications/VLC.app/Contents/MacOS/VLC","-IRC","--rc-host=localhost:4444");
Process p = pb.start();

StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream(), false);
StreamGobbler inputGobbler = new StreamGobbler(p.getInputStream(), false);
errorGobbler.start();
inputGobbler.start();

System.out.println("Waiting: \n"+p.waitFor());
System.out.println("All done here");
//p.destroy();
//System.exit(0);

} catch (IOException ioe) {
ioe.printStackTrace();
} catch (Exception ie) {
ie.printStackTrace();
}
}
}

class StreamGobbler extends Thread {
InputStream is;
boolean discard;
StreamGobbler(InputStream is, boolean discard) {
this.is = is;
this.discard = discard;
}
public void run() {
try {
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line=null;
while ( (line = br.readLine()) != null)
if(!discard)
System.out.println(line);
}
catch (IOException ioe) {
ioe.printStackTrace();
}

}

这是使用 Apache Commons Net 包的 Java 应用程序,我试图连接到在同一台机器上运行的上述应用程序:

public class TelnetTest {
public static void main(String args[]) {


TelnetClient tl = new TelnetClient();
try {
tl.connect("localhost", 4444);
if(tl.isConnected()) {
System.out.println("Connected successfully!");

BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(tl.getOutputStream()));
bw.write("quit");
bw.flush();

} else {
System.err.println("Problem with connection");
}
} catch(Exception e) {
System.err.println("Telnet connection threw an exception: "+e.getMessage());
}
}
}

如果我在终端中使用来自第一个应用程序的命令启动 VLC,则后一个应用程序可以正常工作。同样,我无法在终端中使用“telnet localhost 4444”从终端连接到第一个应用程序。

我能找到的唯一区别是 VLC 的输出。在终端运行时:

[0x2786e8] main interface error: no interface module matched "globalhotkeys,none"
[0x2786e8] main interface error: no suitable interface module
[0x201b28] main libvlc error: interface "globalhotkeys,none" initialization failed
Remote control interface initialized. Type `help' for help.

通过顶级 Java 应用程序执行时:

[0x4009178] main interface error: no interface module matched "globalhotkeys,none"
[0x4009178] main interface error: no suitable interface module
[0x2017a8] main libvlc error: interface "globalhotkeys,none" initialization failed
[0x4009178] main interface error: no suitable interface module
[0x2017a8] main libvlc error: interface "default" initialization failed

有人可以帮我吗?我不知所措。非常感谢。

最佳答案

您可以将 VLC 作为子进程运行,并通过进程输出流向其提供命令。您需要在每个命令后刷新流并 hibernate 一会儿。以下代码不能做所有事情 - 但它确实允许我在 Java 的控制下在 VLC 中播放不同的文件。

     String vlcParameters = String.format(
"-I rc --rc-fake-tty --video-on-top --disable-screensaver --no-video-title-show " +
"--no-mouse-events --no-keyboard-events --no-fullscreen --no-video-deco " +
"--x11-display \"%s\" --video-x %d --video-y %d --width %d --height %d",
":0.0", // X11 display
top, // X
left, //Y
width, //Width
height //Height
);

ProcessBuilder pb = new ProcessBuilder("vlc", vlcParameters);

pb.redirectErrorStream(true);

vlcProcess = pb.start();

// Later - clear current playlist

writer.write("clear\n".getBytes());
writer.flush();
Thread.sleep(10);

String playListCommand = String.format(
"add file://%s\n",
filePath);

writer.write(playListCommand.getBytes());
writer.flush();

Thread.sleep(milliDuration - 10);

注意 - 您将需要另一个线程来读取 VLC 的输出,这样它就不会阻塞:

     Thread inputThread = new Thread(new Runnable()
{

@Override
public void run()
{
InputStream in = vlcProcess.getInputStream();

BufferedReader bufin = new BufferedReader(new InputStreamReader(in));

try
{
while (true)
{
String line = bufin.readLine();

if (line == null)
{
System.out.writeln("End of data from VLC");
}

System.out.writeln("VLC OUTPUT:" + line);
}
}
catch (IOException ex)
{
//...
}
}
},
"VLC stdout reader");

inputThread.start();

关于java - 在 Java 中启动 VLC 并通过 rc 接口(interface)连接到它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2286567/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com