From 07894efd45bb00eba2a230cba1ae308a6d1373b5 Mon Sep 17 00:00:00 2001 From: xufuji456 <839789740@qq.com> Date: Sat, 30 Jan 2021 22:30:08 +0800 Subject: [PATCH] change ThreadPootlUtil to kotlin --- .../main/java/com/frank/ffmpeg/FFmpegCmd.java | 6 +-- .../ffmpeg/activity/MediaHandleActivity.kt | 4 +- .../com/frank/ffmpeg/util/FFmpegUtil.java | 4 +- .../com/frank/ffmpeg/util/ScreenUtil.java | 53 ------------------- .../java/com/frank/ffmpeg/util/ScreenUtil.kt | 50 +++++++++++++++++ .../com/frank/ffmpeg/util/ThreadPoolUtil.java | 14 ----- .../com/frank/ffmpeg/util/ThreadPoolUtil.kt | 14 +++++ .../frank/ffmpeg/view/VideoPreviewBar.java | 2 +- 8 files changed, 72 insertions(+), 75 deletions(-) delete mode 100644 app/src/main/java/com/frank/ffmpeg/util/ScreenUtil.java create mode 100644 app/src/main/java/com/frank/ffmpeg/util/ScreenUtil.kt delete mode 100644 app/src/main/java/com/frank/ffmpeg/util/ThreadPoolUtil.java create mode 100644 app/src/main/java/com/frank/ffmpeg/util/ThreadPoolUtil.kt diff --git a/app/src/main/java/com/frank/ffmpeg/FFmpegCmd.java b/app/src/main/java/com/frank/ffmpeg/FFmpegCmd.java index 88491dc..4456ac4 100644 --- a/app/src/main/java/com/frank/ffmpeg/FFmpegCmd.java +++ b/app/src/main/java/com/frank/ffmpeg/FFmpegCmd.java @@ -49,7 +49,7 @@ public class FFmpegCmd { */ public static void execute(final String[] commands, final OnHandleListener onHandleListener) { mProgressListener = onHandleListener; - ThreadPoolUtil.executeSingleThreadPool(new Runnable() { + ThreadPoolUtil.INSTANCE.executeSingleThreadPool(new Runnable() { @Override public void run() { if (onHandleListener != null) { @@ -76,7 +76,7 @@ public class FFmpegCmd { */ public static void execute(final List commands, final OnHandleListener onHandleListener) { mProgressListener = onHandleListener; - ThreadPoolUtil.executeSingleThreadPool(new Runnable() { + ThreadPoolUtil.INSTANCE.executeSingleThreadPool(new Runnable() { @Override public void run() { if (onHandleListener != null) { @@ -124,7 +124,7 @@ public class FFmpegCmd { * @param onHandleListener onHandleListener */ public static void executeProbe(final String[] commands, final OnHandleListener onHandleListener) { - ThreadPoolUtil.executeSingleThreadPool(new Runnable() { + ThreadPoolUtil.INSTANCE.executeSingleThreadPool(new Runnable() { @Override public void run() { if (onHandleListener != null) { diff --git a/app/src/main/java/com/frank/ffmpeg/activity/MediaHandleActivity.kt b/app/src/main/java/com/frank/ffmpeg/activity/MediaHandleActivity.kt index 7da17cf..efaec71 100644 --- a/app/src/main/java/com/frank/ffmpeg/activity/MediaHandleActivity.kt +++ b/app/src/main/java/com/frank/ffmpeg/activity/MediaHandleActivity.kt @@ -115,7 +115,7 @@ class MediaHandleActivity : BaseActivity() { when (viewId) { R.id.btn_mux//mux:pure video and pure audio -> { - ThreadPoolUtil.executeSingleThreadPool { mediaMux(srcFile) } + ThreadPoolUtil.executeSingleThreadPool (Runnable { mediaMux(srcFile) }) return } R.id.btn_extract_audio//extract audio @@ -130,7 +130,7 @@ class MediaHandleActivity : BaseActivity() { } R.id.btn_dubbing//dubbing -> { - ThreadPoolUtil.executeSingleThreadPool { mediaDubbing(srcFile) } + ThreadPoolUtil.executeSingleThreadPool (Runnable{ mediaDubbing(srcFile) }) return } else -> { diff --git a/app/src/main/java/com/frank/ffmpeg/util/FFmpegUtil.java b/app/src/main/java/com/frank/ffmpeg/util/FFmpegUtil.java index e78c349..09b05b2 100644 --- a/app/src/main/java/com/frank/ffmpeg/util/FFmpegUtil.java +++ b/app/src/main/java/com/frank/ffmpeg/util/FFmpegUtil.java @@ -252,7 +252,7 @@ public class FFmpegUtil { public static String[] addWaterMarkImg(String inputPath, String imgPath, int location, int bitRate, int offsetXY, String outputPath) { String mBitRate = bitRate + "k"; - int offset = ScreenUtil.dp2px(FFmpegApplication.getInstance(), offsetXY); + int offset = ScreenUtil.INSTANCE.dp2px(FFmpegApplication.getInstance(), offsetXY); String overlay = obtainOverlay(offset, offset, location); String waterMarkCmd = "ffmpeg -i %s -i %s -b:v %s -filter_complex %s -preset:v superfast %s"; waterMarkCmd = String.format(waterMarkCmd, inputPath, imgPath, mBitRate, overlay, outputPath); @@ -273,7 +273,7 @@ public class FFmpegUtil { public static String[] addWaterMarkGif(String inputPath, String imgPath, int location, int bitRate, int offsetXY, String outputPath) { String mBitRate = bitRate + "k"; - int offset = ScreenUtil.dp2px(FFmpegApplication.getInstance(), offsetXY); + int offset = ScreenUtil.INSTANCE.dp2px(FFmpegApplication.getInstance(), offsetXY); String overlay = obtainOverlay(offset, offset, location) + ":shortest=1"; String waterMarkCmd = "ffmpeg -i %s -ignore_loop 0 -i %s -b:v %s -filter_complex %s -preset:v superfast %s"; waterMarkCmd = String.format(waterMarkCmd, inputPath, imgPath, mBitRate, overlay, outputPath); diff --git a/app/src/main/java/com/frank/ffmpeg/util/ScreenUtil.java b/app/src/main/java/com/frank/ffmpeg/util/ScreenUtil.java deleted file mode 100644 index 24b89da..0000000 --- a/app/src/main/java/com/frank/ffmpeg/util/ScreenUtil.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.frank.ffmpeg.util; - -import android.content.Context; -import android.util.DisplayMetrics; -import android.view.WindowManager; - -public class ScreenUtil { - - private static DisplayMetrics getDisplayMetrics(Context context) { - WindowManager windowManager = (WindowManager) context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE); - if (windowManager == null) { - return null; - } - DisplayMetrics displayMetrics = new DisplayMetrics(); - windowManager.getDefaultDisplay().getMetrics(displayMetrics); - return displayMetrics; - } - - public static int getScreenWidth(Context context) { - if (context == null) { - return 0; - } - DisplayMetrics displayMetrics = getDisplayMetrics(context); - return displayMetrics != null ? displayMetrics.widthPixels : 0; - } - - public static int getScreenHeight(Context context) { - if (context == null) { - return 0; - } - DisplayMetrics displayMetrics = getDisplayMetrics(context); - return displayMetrics != null ? displayMetrics.heightPixels : 0; - } - - public static int dp2px(Context context, int dpValue) { - if (context == null) { - return 0; - } - DisplayMetrics displayMetrics = getDisplayMetrics(context); - float density = displayMetrics != null ? displayMetrics.density : 0; - return (int) (dpValue * density + 0.5f); - } - - public static int px2dp(Context context, int pxValue) { - if (context == null) { - return 0; - } - DisplayMetrics displayMetrics = getDisplayMetrics(context); - float density = displayMetrics != null ? displayMetrics.density : 0; - return (int) (pxValue / density + 0.5f); - } - -} diff --git a/app/src/main/java/com/frank/ffmpeg/util/ScreenUtil.kt b/app/src/main/java/com/frank/ffmpeg/util/ScreenUtil.kt new file mode 100644 index 0000000..47c41d7 --- /dev/null +++ b/app/src/main/java/com/frank/ffmpeg/util/ScreenUtil.kt @@ -0,0 +1,50 @@ +package com.frank.ffmpeg.util + +import android.content.Context +import android.util.DisplayMetrics +import android.view.WindowManager + +object ScreenUtil { + + private fun getDisplayMetrics(context: Context): DisplayMetrics? { + val windowManager = context.applicationContext.getSystemService(Context.WINDOW_SERVICE) as WindowManager + val displayMetrics = DisplayMetrics() + windowManager.defaultDisplay.getMetrics(displayMetrics) + return displayMetrics + } + + fun getScreenWidth(context: Context?): Int { + if (context == null) { + return 0 + } + val displayMetrics = getDisplayMetrics(context) + return displayMetrics?.widthPixels ?: 0 + } + + fun getScreenHeight(context: Context?): Int { + if (context == null) { + return 0 + } + val displayMetrics = getDisplayMetrics(context) + return displayMetrics?.heightPixels ?: 0 + } + + fun dp2px(context: Context?, dpValue: Int): Int { + if (context == null) { + return 0 + } + val displayMetrics = getDisplayMetrics(context) + val density: Float = displayMetrics?.density ?: 0F + return (dpValue * density + 0.5f).toInt() + } + + fun px2dp(context: Context?, pxValue: Int): Int { + if (context == null) { + return 0 + } + val displayMetrics = getDisplayMetrics(context) + val density: Float = displayMetrics?.density ?: 0F + return (pxValue / density + 0.5f).toInt() + } + +} diff --git a/app/src/main/java/com/frank/ffmpeg/util/ThreadPoolUtil.java b/app/src/main/java/com/frank/ffmpeg/util/ThreadPoolUtil.java deleted file mode 100644 index 17515b9..0000000 --- a/app/src/main/java/com/frank/ffmpeg/util/ThreadPoolUtil.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.frank.ffmpeg.util; - -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; - -public class ThreadPoolUtil { - - public static ExecutorService executeSingleThreadPool(Runnable runnable) { - ExecutorService executor = Executors.newSingleThreadExecutor(); - executor.submit(runnable); - return executor; - } - -} diff --git a/app/src/main/java/com/frank/ffmpeg/util/ThreadPoolUtil.kt b/app/src/main/java/com/frank/ffmpeg/util/ThreadPoolUtil.kt new file mode 100644 index 0000000..7861f68 --- /dev/null +++ b/app/src/main/java/com/frank/ffmpeg/util/ThreadPoolUtil.kt @@ -0,0 +1,14 @@ +package com.frank.ffmpeg.util + +import java.util.concurrent.ExecutorService +import java.util.concurrent.Executors + +object ThreadPoolUtil { + + fun executeSingleThreadPool(runnable: Runnable): ExecutorService { + val executor = Executors.newSingleThreadExecutor() + executor.submit(runnable) + return executor + } + +} diff --git a/app/src/main/java/com/frank/ffmpeg/view/VideoPreviewBar.java b/app/src/main/java/com/frank/ffmpeg/view/VideoPreviewBar.java index 9a809c6..eb09a45 100644 --- a/app/src/main/java/com/frank/ffmpeg/view/VideoPreviewBar.java +++ b/app/src/main/java/com/frank/ffmpeg/view/VideoPreviewBar.java @@ -64,7 +64,7 @@ public class VideoPreviewBar extends RelativeLayout implements HardwareDecode.On txtVideoProgress = view.findViewById(R.id.txt_video_progress); txtVideoDuration = view.findViewById(R.id.txt_video_duration); setListener(); - screenWidth = ScreenUtil.getScreenWidth(context); + screenWidth = ScreenUtil.INSTANCE.getScreenWidth(context); } @Override