0

我尝试制作一个小的 Java 程序,它允许我搜索 twitch 流并在 livestreamer 和 vlc 中打开一个。

所以我有这个方法应该运行 livestreamer。

public static void runLiveStreamer(String channel, String quality) throws IOException{

      String cmd = new String("livestreamer.exe twitch.tv/" + channel + " " + quality);

      System.out.println(cmd);

      Process proc = Runtime.getRuntime().exec(cmd); 

      return;
}

我运行我的代码,没有任何反应,它不会抛出异常或停止工作。如您所见,我有额外的代码行打印出我执行的命令。当我通过 cmd 运行它时,它工作正常。我怎样才能让它工作?

提前感谢您的帮助,对不起我的英语。

4

1 回答 1

0

当您通过 Runtime.getRuntime().exec() 运行某些应用程序时,您需要等到应用程序终止。当 runLiveStreamer() 方法结束时,我正在终止 livestreamer。

更新的代码(这个 while 循环一直在工作,直到 livestreamer 终止,如果你不需要输出,你也可以使用 proc.wait())

public static void runLiveStreamer(String channel, String quality) throws IOException{

      String[] cmd  = new String[]{"livestreamer.exe", "twitch.tv/"+channel, quality};

      Process proc = Runtime.getRuntime().exec(cmd);

      InputStreamReader isr = new InputStreamReader(proc.getInputStream());
      BufferedReader br = new BufferedReader(isr);
      String line=null;
      while ( (line = br.readLine()) != null)
          System.out.println(line);    
      }
于 2015-01-07T18:18:26.753 回答