在Java中启动VLC并通过rc接口连接到它

时间:2010-02-18 05:43:35

标签: java controls telnet vlc

我已经看过这个帖子了,但是我还有一个问题:starting vlc player in java看起来VLC的Java绑定不再处于活动开发状态,并且无论如何都不支持命令行上的所有内容。

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

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

任何人都可以帮助我吗?我不知所措。非常感谢你。

3 个答案:

答案 0 :(得分:5)

您可以将VLC作为子进程运行,并通过进程输出流提供命令。您需要刷新流并在每个命令后休眠一会儿。以下代码 不会做任何事情 - 但它确实允许我在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();

答案 1 :(得分:0)

在另一个论坛上找到解决方案:http://forums.sun.com/thread.jspa?threadID=5145675

在Linux或Mac下运行时,你必须将“--rc-fake-tty”参数传递给VLC。

答案 2 :(得分:0)

由于vlc在rc模式下打开一个新的DOS窗口,因此在writer.flush()期间,代码会抱怨管道已关闭。这也被验证为inputThread打印" VLC OUTPUT:来自VLC"的数据的nullEnd。有没有办法避免它,链接到新打开的vlc rc窗口?

此致

沙希德