
[英]killing a bash process does not kill the process the bash is currently running
[英]How to kill FFmpeg currently running process?
我正在开发一个使用 FFmpeg 修剪视频的应用程序。 当修剪过程已经开始并且我取消它时,FFmpeg 完全终止。 如何仅通过终止当前工作进程来实现?
/* 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.wait();
} catch (IOException | InterruptedException e) {
Log.e(TAG, "kill_ffmpeg: exception failed ",e );
e.printStackTrace();
}
这是我现在一直在使用的代码。 当此代码唤醒时,活动将重新启动,但索引越界除外。
这是我使用 FFmpeg Here实现视频编辑功能的库
这个 FFmpeg 库中有一个方法叫做killRunningProcess()
。 当我使用此方法时,显示一个错误,称为cannot resolve method killRunningProcess()
如何在调用特定代码时在没有内存泄漏和不使应用程序崩溃的情况下杀死进程?
您不必添加或导入任何东西,您只需调用文件中已经声明的方法即可
private void killAllreadyRunningProcess(FFmpeg ffmpeg){
if(ffmpeg.isFFmpegCommandRunning())
ffmpeg.killRunningProcess();
}
使用以下命令从进程对象中获取进程 ID:
public static int getPid(java.lang.Process p) {
int pid = -1;
try {
Field f = p.getClass().getDeclaredField("pid");
f.setAccessible(true);
pid = f.getInt(p);
f.setAccessible(false);
} catch (Throwable e) {
pid = -1;
}
return pid;
}
然后通过调用以下命令向进程发送信号以终止: android.os.Process.sendSignal(pid, 15); // 15 is the value for SIG_TERM
android.os.Process.sendSignal(pid, 15); // 15 is the value for SIG_TERM
ref android.os.Process.sendSignal(pid, 15); // 15 is the value for SIG_TERM
: https : //github.com/WritingMinds/ffmpeg-android-java/issues/33#issuecomment-218816755
解决方案:
例子:
private FFtask fFtask;
// Using your ffmpg Initialize :
fFtask = FFmpeg.getInstance(baseActivity).execute(command, new ExecuteBinaryResponseHandler() {
}
// Cancel Button Click
if (fFtask != null && !fFtask.isProcessCompleted()) {
Log.e(TAG, "on Cancel Click: FFmpeg is RUNNING!!");
fFtask.killRunningProcess();
FileUtils.deleteFileFromPath(outPath);
} else {
Log.i(TAG, "on Cancel Click Not fftask intilization Cancel Dislog not work");
}
private void completeExporting() {
if (dialog != null && dialog.isShowing()) {
try {
dialog.dismiss();
Log.i(TAG, "completeExporting: ");
if (fFtask != null) {
fFtask.killRunningProcess();
}
} catch (Throwable e) {
AppUtils.throwFatalException(e);
}
}
}
销毁:
@Override
public void onDestroy() {
super.onDestroy();
Log.i(TAG, "[onDestroy] ");
completeExporting();
}
使用 FFmpeg :
基于谷歌搜索,我找到了以下解决方案,这对我有用。 我定义了类(FFmpeg_Handling),并且简单地取消了方法。
class FFmpeg_Handling {
private FFmpeg ffmpeg;
void loadFFMpegBinary(Context context) {
try {
if (ffmpeg == null) {
ffmpeg = FFmpeg.getInstance(context);
}
ffmpeg.loadBinary(new LoadBinaryResponseHandler() {
@Override
public void onFailure() {}
@Override
public void onSuccess() {}
});
} catch (Exception ignored) {}
}
void execFFmpegBinary(final String[] command) throws FFmpegCommandAlreadyRunningException {
ffmpeg.execute(command, new ExecuteBinaryResponseHandler() {
@Override
public void onFailure(String s) {}
@Override
public void onSuccess(String s) {}
@Override
public void onProgress(String s) {}
@Override
public void onStart() {}
@Override
public void onFinish() {}
});
}
void cancel(){
ffmpeg.killRunningProcesses();
}
}
然后每当需要时,我通过 instacne 调用取消方法。
实例化:
FFmpeg_Handling fh=new FFmpeg_Handling();
调用取消方法:
fh.cancel();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.