Process is being paused until I close the program

2019-08-06 11:34发布

I made a simple program and UI for it. It has one button for starting ffmpeg.exe to decode a video:

    button.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            ProcessBuilder pb = new ProcessBuilder("D:\\ffmpeg.exe", "-i", "\"D:\\video\\input.mp4\"", "\"output.mp4\"");
            try {
                Process p = pb.start();
            } catch (IOException error) {
                //
            }
        }
    }

The problem is that after clicking the button ffmpeg starts but it doesn't do anything (in task manager it doesn't use cpu - 0%) until I close the program (UI) and then ffmpeg process starts to decode a video (only after closing the program ffmpeg starts using cpu - e.g. 24%)

It's not duplicate: older questions proposed by Andy Thomas doesn't have an answer (solution) for my problem

1条回答
爷、活的狠高调
2楼-- · 2019-08-06 12:27

Your Process blocks the event dispatch thread. Instead, run your ProcessBuilder in the background of a SwingWorker, as shown in this complete example.

    @Override
    protected Integer doInBackground() {
        try {
            ProcessBuilder pb = new ProcessBuilder(
                "D:\\ffmpeg.exe", "-i", "\"D:\\video\\input.mp4\"", "\"output.mp4\""));
        …
    }

Note that the example invokes redirectErrorStream(true), so you should be able to see any errors or prompts from ffmpeg.exe.

查看更多
登录 后发表回答