Java 运行 linux(raspbian) 命令(omxplayer) 并获得输出

标签 java linux terminal raspberry-pi xbmc

我创建了如下程序来执行 linux (raspbian) 命令:“omxplayer”。 但我不知道为什么当我在命令行中输入 omxplayer 并按 Enter 时无法从 omxplayer 获得输出。但输出仅显示在视频末尾。 所以我想在我输入“omxplayer [video_name]”并在我的程序中点击“Enter”后立即获得输出。 就像我在 linux 中直接输入命令行(终端)一样。 这是我的代码:

public class testprog {
    public static void main(String args[]) throws IOException {

        String in = "";
        while(in!="exit")
        {
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            in = reader.readLine();
            runCommand(in);
        }
    }

    public static void runCommand(String command)
    {
           String s;
         Process p;
         try {
             System.out.println("run command " + command);
             p = Runtime.getRuntime().exec(new String[]{"bash", "-c",command});

             MyInputStreamReader reader1 = new MyInputStreamReader(p.getInputStream());
             reader1.setTag("in");
             reader1.start();

             MyInputStreamReader reader2 = new MyInputStreamReader(p.getErrorStream());
             reader2.setTag("in");
             reader2.start();


             p.waitFor();
             System.out.println ("exit: " + p.exitValue());
             p.destroy();
         } catch (Exception e) {}

    }
}

class MyInputStreamReader extends Thread{
    boolean isStop = false;
    ReadEventHandler handler;
    String tag;
    InputStream in;
    public MyInputStreamReader(InputStream in)
    {
        this.in = in;
    }

    public void setHandler(ReadEventHandler handler) {
        this.handler = handler;
    }
    public void setTag(String tag)
    {
        this.tag = tag;
    }

    public void run()
    {
        byte[] buff = new byte[8192];
        while (true) {
            //String line;
            try {
                int len  = in.read(buff);
                if (len == -1)
                {
                    return;
                }

                String line = new String(buff, 0, len);
                if (handler!=null)
                    handler.onReceived(line);
                System.out.println(tag +" " + line);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

    }
    public void dispose()
    {
        this.isStop = true;
    }
    public interface ReadEventHandler
    {
        void onReceived(String line);
    }
}

非常感谢任何回复。谢谢

最佳答案

你检查了吗?

http://javedmandary.blogspot.com/2014/01/firing-up-raspberry-pi-omxplayer-using.html

我想这就是您要查找的代码。

关于Java 运行 linux(raspbian) 命令(omxplayer) 并获得输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27224852/

相关文章:

java - 无法从 war 中获取文件

java - 如何在java或python中根据一张图像生成不同颜色的图像?

java - 在非 spring 管理的 java 类中使用 Autowiring 的单例 bean

bash - 使用命令行输出将 AWK 模式与变量一起使用

vim - 终端vi模式: Scrolling through previous lines and outputs

java - 如何在java中使用正则表达式分割字符串

c# - sched_setscheduler 的 P/Invoke 签名

linux - 如何按列对一组数据进行排序?

linux - 使用 cmake 链接两个相似的库

linux - 在终端中使用带有 curl 的 "if"语句?