callback progress to application

callback progress to application
pull/166/head
xufulong 5 years ago
parent 6b7f127221
commit 00860718b1
  1. 38
      app/src/main/java/com/frank/ffmpeg/FFmpegCmd.java
  2. 7
      app/src/main/java/com/frank/ffmpeg/handler/FFmpegHandler.java

@ -5,8 +5,13 @@ import android.util.Log;
import com.frank.ffmpeg.listener.OnHandleListener; 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 java.util.List;
import androidx.annotation.IntDef;
/** /**
* The JNI interface of handling FFmpeg command * The JNI interface of handling FFmpeg command
* Created by frank on 2018/1/23 * Created by frank on 2018/1/23
@ -21,12 +26,28 @@ public class FFmpegCmd {
private final static int RESULT_ERROR = 0; 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 * Execute FFmpeg command
* @param commands the String array of command * @param commands the String array of command
* @param onHandleListener the callback for executing command * @param onHandleListener the callback for executing command
*/ */
public static void execute(final String[] commands, final OnHandleListener onHandleListener) { public static void execute(final String[] commands, final OnHandleListener onHandleListener) {
mProgressListener = onHandleListener;
new Thread(new Runnable() { new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {
@ -38,6 +59,7 @@ public class FFmpegCmd {
if (onHandleListener != null) { if (onHandleListener != null) {
onHandleListener.onEnd(result, null); onHandleListener.onEnd(result, null);
} }
mProgressListener = null;
} }
}).start(); }).start();
} }
@ -122,8 +144,18 @@ public class FFmpegCmd {
private native static String handleProbe(String[] commands); private native static String handleProbe(String[] commands);
public static void onProgressCallback(int position, int duration, int state) { public static void onProgressCallback(int position, int duration, @FFmpegState int state) {
Log.e("FFmpegCmd", "onProgress position=" + position + "--duration=" + duration); 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);
}
}
} }
} }

@ -20,6 +20,8 @@ public class FFmpegHandler {
public final static int MSG_BEGIN = 9012; 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_FINISH = 1112;
public final static int MSG_CONTINUE = 2012; public final static int MSG_CONTINUE = 2012;
@ -54,6 +56,11 @@ public class FFmpegHandler {
mHandler.obtainMessage(MSG_BEGIN).sendToTarget(); mHandler.obtainMessage(MSG_BEGIN).sendToTarget();
} }
@Override
public void onProgress(int progress, int duration) {
mHandler.obtainMessage(MSG_PROGRESS, progress, duration).sendToTarget();
}
@Override @Override
public void onEnd(int resultCode, String resultMsg) { public void onEnd(int resultCode, String resultMsg) {
Log.i(TAG, "handle onEnd..."); Log.i(TAG, "handle onEnd...");

Loading…
Cancel
Save