0

I am developing an encoder with java swing and ffmpeg. I created a GUI interface in which I specify my inputs (devices, frame rate, bitrate..). Then I call ffmpeg to encode and stream.

My problem is that the encoding class is well executed from a main class but it is blocked when called from the swing interface (specifically jButtonactionperformed()).

Can anyone help me?

here is my button action

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
    Encode s = new Encode();
    s.Encode(cmdLine);
}

and here is my encoding method

public void Encode(String cmdLine) {
    try {
        Process p2 = Runtime.getRuntime().exec(cmdLine);
        //logProcessOutputAndErrors(p2); 
    }
    catch(Exception ex) {
        ex.printStackTrace();
    }
}

Ps: Cmdline is the command i collect from inputs

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • So, you want a tutorial on threads from us? – John Dvorak Dec 27 '12 at 14:00
  • you should start the ffmpeg task in a separate thread to not block the ui thread. – clamp Dec 27 '12 at 14:03
  • in fact i execute the ffmpeg command in a seperate class but i call this class in the jButtonaction performed. @Jan Dvorak: i read about threads but i couldn't apply it. if you can explain it more please – user1932255 Dec 27 '12 at 14:16
  • here is my button action private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) { Encode s= new Encode(); s.Encode(cmdLine); } and here is my encoding method public void Encode( String cmdLine) { try { Process p2 = Runtime.getRuntime().exec(cmdLine); //logProcessOutputAndErrors(p2); } catch (Exception ex) { ex.printStackTrace(); } } Ps: Cmdline is the command i collect from inputs – user1932255 Dec 27 '12 at 14:23
  • Any suggestions pleaase :( – user1932255 Dec 27 '12 at 15:01
  • 1
    1) *"here is my button action"* For better help sooner, post an [SSCCE](http://sscce.org/) as an [edit](http://stackoverflow.com/posts/14055818/edit) to the question. 2) If sticking with the `Process`, implement all the recommendations of [When Runtime.exec() won't](http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html). 3) Why not use jffmpeg and side-step the (more trouble, less control) `Process` altogether? 4) Add @clamp (or whoever) to notify them of a new comment. – Andrew Thompson Dec 27 '12 at 15:02

1 Answers1

2

First, you convert your Encode method into a Runnable class.

public class Encode implements Runnable {

    protected String    cmdLine;

    public Encode(String cmdLine) {
        this.cmdLine = cmdLine;
    }

    @Override
    public void run() {
        try {
            Process p2 = Runtime.getRuntime().exec(cmdLine);
            // logProcessOutputAndErrors(p2);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

}

Then, you instantiate the class as a Thread, and start it.

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
    Encode s = new Encode(cmdLine);
    new Thread(s).start();
}
Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
  • Thanks for your answer @Gilbert le Blanc. I copy and paste this but i have the same result. my streaming server receive my selected options(rate..) but the (Netstream.buffer.flush) until i close the interface. the process is no more blocked and ffmpeg start bufferfull etc.. :( – user1932255 Dec 27 '12 at 15:42
  • @user1932255: There's more to your problem then blocking the Event Dispatch Thread. You have to get the audio buffer back to your GUI in some way, and let the GUI know that the buffer is complete. I'm afraid that's more that can be dealt with on Stack Overflow. – Gilbert Le Blanc Dec 27 '12 at 15:48
  • The problem that when i execute the same code in a main class (not a GUI) it is fine and i get my streaming (buffer full) . that is why i thought about the event dispatch problem – user1932255 Dec 27 '12 at 15:54
  • The problem that when i execute the same code in a main class (not a GUI) it is fine and i get my streaming (buffer full) . that is why i thought about the event dispatch problem – user1932255 Dec 27 '12 at 16:00