From 00860718b1646a02f52e6898d750bdf878395b98 Mon Sep 17 00:00:00 2001 From: xufulong <839789740@qq.com> Date: Tue, 16 Jun 2020 00:25:42 +0800 Subject: [PATCH] callback progress to application callback progress to application --- .../main/java/com/frank/ffmpeg/FFmpegCmd.java | 38 +++++++++++++++++-- .../frank/ffmpeg/handler/FFmpegHandler.java | 7 ++++ 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/frank/ffmpeg/FFmpegCmd.java b/app/src/main/java/com/frank/ffmpeg/FFmpegCmd.java index 9bdb5bc..d39aafa 100644 --- a/app/src/main/java/com/frank/ffmpeg/FFmpegCmd.java +++ b/app/src/main/java/com/frank/ffmpeg/FFmpegCmd.java @@ -5,8 +5,13 @@ import android.util.Log; import com.frank.ffmpeg.listener.OnHandleListener; +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; import java.util.List; +import androidx.annotation.IntDef; + /** * The JNI interface of handling FFmpeg command * Created by frank on 2018/1/23 @@ -21,12 +26,28 @@ public class FFmpegCmd { private final static int RESULT_ERROR = 0; + private static OnHandleListener mProgressListener; + + private static final int STATE_INIT = 0; + + private static final int STATE_RUNNING = 1; + + private static final int STATE_FINISH = 2; + + private static final int STATE_ERROR = 3; + + @Documented + @Retention(RetentionPolicy.SOURCE) + @IntDef({STATE_INIT, STATE_RUNNING, STATE_FINISH, STATE_ERROR}) + public @interface FFmpegState {} + /** * Execute FFmpeg command * @param commands the String array of command * @param onHandleListener the callback for executing command */ public static void execute(final String[] commands, final OnHandleListener onHandleListener) { + mProgressListener = onHandleListener; new Thread(new Runnable() { @Override public void run() { @@ -38,6 +59,7 @@ public class FFmpegCmd { if (onHandleListener != null) { onHandleListener.onEnd(result, null); } + mProgressListener = null; } }).start(); } @@ -122,8 +144,18 @@ public class FFmpegCmd { private native static String handleProbe(String[] commands); - public static void onProgressCallback(int position, int duration, int state) { - Log.e("FFmpegCmd", "onProgress position=" + position + "--duration=" + duration); - + public static void onProgressCallback(int position, int duration, @FFmpegState int state) { + Log.e("FFmpegCmd", "onProgress position=" + position + + "--duration=" + duration + "--state=" + state); + if (mProgressListener != null) { + if (position > 0 && duration > 0) { + int progress = position * 100 / duration; + if (progress < 100 || state == STATE_FINISH || state == STATE_ERROR) { + mProgressListener.onProgress(progress, duration); + } + } else { + mProgressListener.onProgress(position, duration); + } + } } } \ No newline at end of file diff --git a/app/src/main/java/com/frank/ffmpeg/handler/FFmpegHandler.java b/app/src/main/java/com/frank/ffmpeg/handler/FFmpegHandler.java index 0e185a2..8bac56f 100644 --- a/app/src/main/java/com/frank/ffmpeg/handler/FFmpegHandler.java +++ b/app/src/main/java/com/frank/ffmpeg/handler/FFmpegHandler.java @@ -20,6 +20,8 @@ public class FFmpegHandler { public final static int MSG_BEGIN = 9012; + public final static int MSG_PROGRESS = 1002; + public final static int MSG_FINISH = 1112; public final static int MSG_CONTINUE = 2012; @@ -54,6 +56,11 @@ public class FFmpegHandler { mHandler.obtainMessage(MSG_BEGIN).sendToTarget(); } + @Override + public void onProgress(int progress, int duration) { + mHandler.obtainMessage(MSG_PROGRESS, progress, duration).sendToTarget(); + } + @Override public void onEnd(int resultCode, String resultMsg) { Log.i(TAG, "handle onEnd...");