【问题标题】:how to stop commandline process in android如何在android中停止命令行进程
【发布时间】:2016-01-15 11:06:48
【问题描述】:

请帮助我,我必须在我的应用程序中使用 ffmpeg cmdline。我构建了 ffmpeg 二进制文件,现在我可以开始使用 ffmpeg,如下所示:

public class FfmpegCmd {
...
   public ProcessRunnable create(){
     final List<String> cmd = new LinkedList<String>();

     ... // filling ffmpeg parameters

     final ProcessBuilder pb = new ProcessBuilder(cmd);
     return new ProcessRunnable(pb);
}
...
}

我开始 ffmpeg 转换如下:

final FfmpegCmd task = new FfmpegCmd();

new AsyncTask<Void, Void, Void>() {

            @Override
            protected Void doInBackground(Void... arg0) {
                task.create().run();
                return null;
            }

            @Override
            protected void onPostExecute(Void result) {                 
                Toast.makeText(MainActivity.this, "Ffmpeg cmd complete.", Toast.LENGTH_SHORT).show();
            }

        }.execute();

它工作正常。但我想增加终止 ffmpeg 命令执行的能力。我怎样才能做到这一点?如何终止 ProcessRunnable?

我需要“发送 CTRL+C 到 ProcessRunnable”之类的东西

【问题讨论】:

    标签: android process termination


    【解决方案1】:

    解决如下:

    Process ffmpeg = Runtime.getRuntime().exec(mFfmpegInstallPath + " -y -i
    rtsp://192.168.1.254/xxx.mov -b 2M -vcodec mpeg4 /sdcard/video.avi\n");
    

    ...

    private void kill_ffmpeg(){
        Log.d("output: ",ffmpeg.toString());
    
        /* define PID */
        try {
            String pid = ffmpeg.toString();
            pid = pid.substring(pid.indexOf("[") + 1, pid.indexOf("]"));
            Process process = Runtime.getRuntime().exec("kill -2 " + pid);
    
            process.waitFor();
            ffmpeg.waitFor();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-25
      • 1970-01-01
      • 1970-01-01
      • 2012-05-28
      • 2013-11-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多