From 174a13a3c6b19f03c3995d6940cba629a59663c2 Mon Sep 17 00:00:00 2001 From: xufuji456 <839789740@qq.com> Date: Sun, 1 Aug 2021 21:31:06 +0800 Subject: [PATCH] using work thread to parse lrc, and callback to main thread --- .../ffmpeg/activity/AudioPlayActivity.kt | 32 ++++++++++++------- .../java/com/frank/ffmpeg/model/AudioBean.kt | 2 ++ .../java/com/frank/ffmpeg/tool/LrcParser.kt | 5 ++- .../java/com/frank/ffmpeg/view/LrcView.kt | 2 +- 4 files changed, 28 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/com/frank/ffmpeg/activity/AudioPlayActivity.kt b/app/src/main/java/com/frank/ffmpeg/activity/AudioPlayActivity.kt index c572f20..2943b73 100644 --- a/app/src/main/java/com/frank/ffmpeg/activity/AudioPlayActivity.kt +++ b/app/src/main/java/com/frank/ffmpeg/activity/AudioPlayActivity.kt @@ -15,12 +15,14 @@ import com.frank.ffmpeg.R import com.frank.ffmpeg.handler.FFmpegHandler import com.frank.ffmpeg.handler.FFmpegHandler.MSG_FINISH import com.frank.ffmpeg.listener.OnLrcListener +import com.frank.ffmpeg.model.AudioBean import com.frank.ffmpeg.model.MediaBean import com.frank.ffmpeg.util.FFmpegUtil import com.frank.ffmpeg.util.TimeUtil import com.frank.ffmpeg.model.LrcLine import com.frank.ffmpeg.tool.LrcLineTool import com.frank.ffmpeg.tool.LrcParser +import com.frank.ffmpeg.util.ThreadPoolUtil import com.frank.ffmpeg.view.LrcView import java.io.File @@ -63,9 +65,10 @@ class AudioPlayActivity : AppCompatActivity() { MSG_FINISH -> { if (msg.obj == null) return val result = msg.obj as MediaBean - txtTitle?.text = result.audioBean?.title - txtArtist?.text = result.audioBean?.artist - val lyrics = result.audioBean?.lyrics + val audioBean = result.audioBean + txtTitle?.text = audioBean?.title + txtArtist?.text = audioBean?.artist + val lyrics = audioBean?.lyrics if (lyrics != null) { val lrcList = arrayListOf() for (i in lyrics.indices) { @@ -75,6 +78,8 @@ class AudioPlayActivity : AppCompatActivity() { } LrcLineTool.sortLyrics(lrcList) lrcView?.setLrc(lrcList) + } else if (audioBean?.lrcLineList != null) { + lrcView?.setLrc(audioBean.lrcLineList!!) } } } @@ -156,14 +161,19 @@ class AudioPlayActivity : AppCompatActivity() { } if (!lrcPath.isNullOrEmpty() && File(lrcPath).exists()) { // should parsing in work thread - val lrcParser = LrcParser() - val lrcInfo = lrcParser.readLrc(lrcPath) - Log.e(TAG, "title=${lrcInfo?.title},album=${lrcInfo?.album},artist=${lrcInfo?.artist}") - if (lrcInfo?.lrcLineList != null) { - lrcView?.setLrc(lrcInfo.lrcLineList!!) - } - txtTitle?.text = lrcInfo?.title - txtArtist?.text = lrcInfo?.artist + ThreadPoolUtil.executeSingleThreadPool(Runnable { + val lrcParser = LrcParser() + val lrcInfo = lrcParser.readLrc(lrcPath) + Log.e(TAG, "title=${lrcInfo?.title},album=${lrcInfo?.album},artist=${lrcInfo?.artist}") + val mediaBean = MediaBean() + val audioBean = AudioBean() + audioBean.title = lrcInfo?.title + audioBean.album = lrcInfo?.album + audioBean.artist = lrcInfo?.artist + audioBean.lrcLineList = lrcInfo?.lrcLineList + mediaBean.audioBean = audioBean + mHandler.obtainMessage(MSG_FINISH, mediaBean).sendToTarget() + }) } else { val ffmpegHandler = FFmpegHandler(mHandler) val commandLine = FFmpegUtil.probeFormat(path) diff --git a/app/src/main/java/com/frank/ffmpeg/model/AudioBean.kt b/app/src/main/java/com/frank/ffmpeg/model/AudioBean.kt index 015f782..a1a10b7 100644 --- a/app/src/main/java/com/frank/ffmpeg/model/AudioBean.kt +++ b/app/src/main/java/com/frank/ffmpeg/model/AudioBean.kt @@ -35,4 +35,6 @@ class AudioBean { var lyrics: List? = null + var lrcLineList: List? = null + } diff --git a/app/src/main/java/com/frank/ffmpeg/tool/LrcParser.kt b/app/src/main/java/com/frank/ffmpeg/tool/LrcParser.kt index e1563c8..0b55bee 100644 --- a/app/src/main/java/com/frank/ffmpeg/tool/LrcParser.kt +++ b/app/src/main/java/com/frank/ffmpeg/tool/LrcParser.kt @@ -129,10 +129,12 @@ class LrcParser { var currentTime: Long = 0 while (matcher.find()) { val groupCount = matcher.groupCount() + var currentTimeStr: String? = "" for (index in 0 until groupCount) { val timeStr = matcher.group(index) if (index == 0) { - currentTime = TimeUtil.timeStrToLong(timeStr.substring(1, timeStr.length - 1)) + currentTimeStr = timeStr.substring(1, timeStr.length - 1) + currentTime = TimeUtil.timeStrToLong(currentTimeStr) } } val content = pattern.split(str) @@ -141,6 +143,7 @@ class LrcParser { currentContent = content[content.size - 1] } val lrcLine = LrcLine() + lrcLine.timeString = currentTimeStr lrcLine.startTime = currentTime lrcLine.endTime = currentTime + 10 * 1000 lrcLine.content = currentContent diff --git a/app/src/main/java/com/frank/ffmpeg/view/LrcView.kt b/app/src/main/java/com/frank/ffmpeg/view/LrcView.kt index 1acc091..f120bb6 100644 --- a/app/src/main/java/com/frank/ffmpeg/view/LrcView.kt +++ b/app/src/main/java/com/frank/ffmpeg/view/LrcView.kt @@ -243,7 +243,7 @@ class LrcView(context: Context, attr: AttributeSet) : View(context, attr) { companion object { - private const val mSeekLineTextSize = 15 + private const val mSeekLineTextSize = 25 private const val mSeekLineColor = Color.RED