将进程传递给子类

时间:2014-12-16 08:52:54

标签: java ffmpeg

我想将一个进程传递给一个子类,所以我可以杀死它,但我无法弄清楚如何传递该进程。我不确定如何存储它所以我可以将它返回到表单并能够调用子类方法来杀死它。这是我的课程

package my.mashformcnts;
import java.io.IOException;
import java.util.Scanner;
import java.util.regex.Pattern;


/**
 *
 * @author brett
 */
public class MashRocks {

    public static Process startThread(MashFormCnts mashFormCnts) throws IOException {
        ProcessBuilder pb = new ProcessBuilder("ffmpeg", "-i", "C:\\Users\\brett\\Documents\\Telegraph_Road.mp4", "C:\\Users\\brett\\Documents\\out.mp4");

        //Here is where i would like to name and store the Process


        final Process p = pb.start();
        // create a new thread to get progress from ffmpeg command , override  
        // it's run method, and start it!  
        Thread t = new Thread() {
            @Override
            public void run() {
                Scanner sc = new Scanner(p.getErrorStream());
                // Find duration  
                Pattern durPattern = Pattern.compile("(?<=Duration: )[^,]*");
                String dur = sc.findWithinHorizon(durPattern, 0);
                if (dur == null) {
                    throw new RuntimeException("Could not parse duration.");
                }
                String[] hms = dur.split(":");
                double totalSecs = Integer.parseInt(hms[0]) * 3600 + Integer.parseInt(hms[1]) * 60 + Double.parseDouble(hms[2]);
                System.out.println("Total duration: " + totalSecs + " seconds.");
                // Find time as long as possible.  
                Pattern timePattern = Pattern.compile("(?<=time=)[\\d:.]*");
                String match;
                String[] matchSplit;
                //MashForm pgbar = new MashForm();
                while (null != (match = sc.findWithinHorizon(timePattern, 0))) {
                    matchSplit = match.split(":");
                    double progress = (Integer.parseInt(matchSplit[0]) * 3600 + Integer.parseInt(matchSplit[1]) * 60 + Double.parseDouble(matchSplit[2])) / totalSecs;
                    int prog = (int) (progress * 100);
                    mashFormCunts.setbar(prog);
                }
            }
        };
       t.start();
       return p;
    }
   public synchronized static void stop(Thread t) throws IOException{
           Runtime.getRuntime().exec("taskkill /F /IM ffmpeg.exe");  
            t = null;
          //t.interrupt();



   }
}
   class killMash extends MashRocks{
    public static void Kfpeg(Process p){

      p.destroyForcibly();
    }
}

所以这些是我的课程。我很新。

接下来在表单上有一个事件Listener,所以当我点击这个时我想用Thread t杀死ffmpeg proecess:

  private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        Thread n = Thread.currentThread();
        System.out.print(n);
        try {
            //MashRocks.stop(n);
             //This isnt working but i think its closer
             killMash.Kfpeg(MashRocks.startThread(this)); 
//Not Sure what to do here
  //here is where i want to pass the process sorry for the typo
  killMash.kfpeg(p); 
            } catch (IOException ex) {
                Logger.getLogger(MashFormCunts.class.getName()).log(Level.SEVERE, null, ex);
            }

        }   

任何帮助都是令人敬畏的欢呼

1 个答案:

答案 0 :(得分:1)

我不知道为什么你想让一切都变得静止但如果你需要一种可能性就是将过程存储为一个类变量:

public class MashRocks {

    protected static Process process = null;

    public static Process startThread(MashFormCnts mashFormCnts) throws IOException {
    ProcessBuilder pb = new ProcessBuilder("ffmpeg", "-i", "C:\\Users\\brett\\Documents\\Telegraph_Road.mp4", "C:\\Users\\brett\\Documents\\out.mp4");

    final Process p = pb.start();
    MashRocks.process = p;
    ....
    }
}

你的停止方法可能是:

public synchronized static void stop() throws IOException{
    if(MashRocks.process != null)
    {
        MashRocks.process.destroy();
    }
}

你的子课程:

class KillMash extends MashRocks{
    public static void Kfpeg() throws IOException{
        KillMash.stop();
    }
}