diff --git a/app/src/main/cpp/ffmpeg_cmd.c b/app/src/main/cpp/ffmpeg_cmd.c index d5d5187..a12f496 100644 --- a/app/src/main/cpp/ffmpeg_cmd.c +++ b/app/src/main/cpp/ffmpeg_cmd.c @@ -54,6 +54,16 @@ FFMPEG_FUNC(void, cancelTaskJni, jint cancel) { cancel_task(cancel); } +void msg_callback(const char* format, va_list args) { + if (ff_env && msg_method) { + char *ff_msg = (char*) malloc(sizeof(char) * 1024); + vsprintf(ff_msg, format, args); + jstring jstr = (*ff_env)->NewStringUTF(ff_env, ff_msg); + (*ff_env)->CallStaticVoidMethod(ff_env, ff_class, msg_method, jstr); + free(ff_msg); + } +} + void log_callback(void* ptr, int level, const char* format, va_list args) { switch (level) { case AV_LOG_WARNING: @@ -61,16 +71,13 @@ void log_callback(void* ptr, int level, const char* format, va_list args) { break; case AV_LOG_INFO: ALOGI(FFMPEG_TAG, format, args); + if (format && strncmp("silence", format, 7) == 0) { + msg_callback(format, args); + } break; case AV_LOG_ERROR: ALOGE(FFMPEG_TAG, format, args); - if (ff_env && msg_method) { - char *ff_msg = (char*) malloc(sizeof(char) * 1024); - vsprintf(ff_msg, format, args); - jstring jstr = (*ff_env)->NewStringUTF(ff_env, ff_msg); - (*ff_env)->CallStaticVoidMethod(ff_env, ff_class, msg_method, jstr); - free(ff_msg); - } + msg_callback(format, args); break; default: break; diff --git a/app/src/main/java/com/frank/ffmpeg/FFmpegCmd.java b/app/src/main/java/com/frank/ffmpeg/FFmpegCmd.java index 43e30ec..bf6dc8f 100644 --- a/app/src/main/java/com/frank/ffmpeg/FFmpegCmd.java +++ b/app/src/main/java/com/frank/ffmpeg/FFmpegCmd.java @@ -180,6 +180,11 @@ public class FFmpegCmd { public static void onMsgCallback(String msg) { if (msg != null && !msg.isEmpty()) { Log.e(TAG, "from native msg=" + msg); + + // silence detect callback + if (msg.startsWith("silence") && mProgressListener != null) { + mProgressListener.onMsg(msg); + } } } diff --git a/app/src/main/java/com/frank/ffmpeg/activity/AudioHandleActivity.kt b/app/src/main/java/com/frank/ffmpeg/activity/AudioHandleActivity.kt index 2b9c0ed..0f7eee5 100644 --- a/app/src/main/java/com/frank/ffmpeg/activity/AudioHandleActivity.kt +++ b/app/src/main/java/com/frank/ffmpeg/activity/AudioHandleActivity.kt @@ -11,6 +11,7 @@ import android.util.Log import android.view.View import android.widget.LinearLayout import android.widget.TextView +import android.widget.Toast import java.io.File import java.util.ArrayList @@ -22,9 +23,11 @@ import com.frank.ffmpeg.handler.FFmpegHandler import com.frank.ffmpeg.util.FFmpegUtil import com.frank.ffmpeg.util.FileUtil +import com.frank.ffmpeg.handler.FFmpegHandler.MSG_INFO import com.frank.ffmpeg.handler.FFmpegHandler.MSG_BEGIN import com.frank.ffmpeg.handler.FFmpegHandler.MSG_FINISH import com.frank.ffmpeg.handler.FFmpegHandler.MSG_PROGRESS +import java.lang.StringBuilder /** * Using ffmpeg command to handle audio @@ -43,6 +46,7 @@ class AudioHandleActivity : BaseActivity() { private val outputPath1 = PATH + File.separator + "output1.mp3" private val outputPath2 = PATH + File.separator + "output2.mp3" private var isJointing = false + private var infoBuilder: StringBuilder? = null @SuppressLint("HandlerLeak") private val mHandler = object : Handler() { @@ -61,6 +65,11 @@ class AudioHandleActivity : BaseActivity() { FileUtil.deleteFile(outputPath1) FileUtil.deleteFile(outputPath2) } + if (infoBuilder != null) { + Toast.makeText(this@AudioHandleActivity, + infoBuilder.toString(), Toast.LENGTH_LONG).show() + infoBuilder = null + } } MSG_PROGRESS -> { val progress = msg.arg1 @@ -72,6 +81,10 @@ class AudioHandleActivity : BaseActivity() { txtProgress!!.visibility = View.INVISIBLE } } + MSG_INFO -> { + if (infoBuilder == null) infoBuilder = StringBuilder() + infoBuilder?.append(msg.obj) + } else -> { } } 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 290004e..acc3250 100644 --- a/app/src/main/java/com/frank/ffmpeg/handler/FFmpegHandler.java +++ b/app/src/main/java/com/frank/ffmpeg/handler/FFmpegHandler.java @@ -8,6 +8,8 @@ import com.frank.ffmpeg.listener.OnHandleListener; import com.frank.ffmpeg.model.MediaBean; import com.frank.ffmpeg.tool.JsonParseTool; +import org.jetbrains.annotations.NotNull; + import java.util.List; /** @@ -26,7 +28,9 @@ public class FFmpegHandler { public final static int MSG_CONTINUE = 2012; - public final static int MSG_TOAST = 4562; + public final static int MSG_TOAST = 5432; + + public final static int MSG_INFO = 2222; private Handler mHandler; @@ -56,6 +60,11 @@ public class FFmpegHandler { mHandler.obtainMessage(MSG_BEGIN).sendToTarget(); } + @Override + public void onMsg(@NotNull String msg) { + mHandler.obtainMessage(MSG_INFO, msg).sendToTarget(); + } + @Override public void onProgress(int progress, int duration) { mHandler.obtainMessage(MSG_PROGRESS, progress, duration).sendToTarget(); @@ -97,6 +106,11 @@ public class FFmpegHandler { mHandler.obtainMessage(MSG_BEGIN).sendToTarget(); } + @Override + public void onMsg(@NotNull String msg) { + + } + @Override public void onProgress(int progress, int duration) { mHandler.obtainMessage(MSG_PROGRESS, progress, duration).sendToTarget(); @@ -130,6 +144,11 @@ public class FFmpegHandler { mHandler.obtainMessage(MSG_BEGIN).sendToTarget(); } + @Override + public void onMsg(@NotNull String msg) { + + } + @Override public void onProgress(int progress, int duration) { diff --git a/app/src/main/java/com/frank/ffmpeg/listener/OnHandleListener.kt b/app/src/main/java/com/frank/ffmpeg/listener/OnHandleListener.kt index 026be1b..592852e 100644 --- a/app/src/main/java/com/frank/ffmpeg/listener/OnHandleListener.kt +++ b/app/src/main/java/com/frank/ffmpeg/listener/OnHandleListener.kt @@ -6,6 +6,7 @@ package com.frank.ffmpeg.listener */ interface OnHandleListener { fun onBegin() + fun onMsg(msg: String) fun onProgress(progress: Int, duration: Int) fun onEnd(resultCode: Int, resultMsg: String) }