From cdb33387e244127281fce9d61b592491010f1821 Mon Sep 17 00:00:00 2001 From: xufuji456 <839789740@qq.com> Date: Sun, 1 Aug 2021 20:25:44 +0800 Subject: [PATCH] parse and display external lrc --- .../ffmpeg/activity/AudioPlayActivity.kt | 26 ++++++++++++++++--- .../com/frank/ffmpeg/activity/BaseActivity.kt | 13 +++++++--- .../java/com/frank/ffmpeg/model/LrcInfo.kt | 5 +--- .../java/com/frank/ffmpeg/view/LrcView.kt | 4 +-- 4 files changed, 35 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 db6d99c..c572f20 100644 --- a/app/src/main/java/com/frank/ffmpeg/activity/AudioPlayActivity.kt +++ b/app/src/main/java/com/frank/ffmpeg/activity/AudioPlayActivity.kt @@ -20,7 +20,9 @@ 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.view.LrcView +import java.io.File class AudioPlayActivity : AppCompatActivity() { @@ -147,9 +149,26 @@ class AudioPlayActivity : AppCompatActivity() { private fun initLrc() { if (path.isNullOrEmpty()) return - val ffmpegHandler = FFmpegHandler(mHandler) - val commandLine = FFmpegUtil.probeFormat(path) - ffmpegHandler.executeFFprobeCmd(commandLine) + var lrcPath: String? = null + if (path!!.contains(".")) { + lrcPath = path!!.substring(0, path!!.lastIndexOf(".")) + ".lrc" + Log.e(TAG, "lrcPath=$lrcPath") + } + 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 + } else { + val ffmpegHandler = FFmpegHandler(mHandler) + val commandLine = FFmpegUtil.probeFormat(path) + ffmpegHandler.executeFFprobeCmd(commandLine) + } } private fun isPlaying() :Boolean { @@ -159,6 +178,7 @@ class AudioPlayActivity : AppCompatActivity() { override fun onStop() { super.onStop() try { + mHandler.removeCallbacksAndMessages(null) audioPlayer.stop() audioPlayer.release() } catch (e: Exception) { diff --git a/app/src/main/java/com/frank/ffmpeg/activity/BaseActivity.kt b/app/src/main/java/com/frank/ffmpeg/activity/BaseActivity.kt index da35bd3..0413d8e 100644 --- a/app/src/main/java/com/frank/ffmpeg/activity/BaseActivity.kt +++ b/app/src/main/java/com/frank/ffmpeg/activity/BaseActivity.kt @@ -16,6 +16,7 @@ import android.widget.Toast import com.frank.ffmpeg.R import com.frank.ffmpeg.util.ContentUtil +import java.lang.Exception /** * base Activity @@ -74,10 +75,14 @@ abstract class BaseActivity : AppCompatActivity(), View.OnClickListener { override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super.onActivityResult(requestCode, resultCode, data) - if (data != null && data.data != null) { - val filePath = ContentUtil.getPath(this, data.data) - Log.i(TAG, "filePath=" + filePath!!) - onSelectedFile(filePath) + try { + if (data != null && data.data != null) { + val filePath = ContentUtil.getPath(this, data.data!!) + Log.i(TAG, "filePath=" + filePath!!) + onSelectedFile(filePath) + } + } catch (e : Exception) { + e.printStackTrace() } } diff --git a/app/src/main/java/com/frank/ffmpeg/model/LrcInfo.kt b/app/src/main/java/com/frank/ffmpeg/model/LrcInfo.kt index 4b9383c..a7a096f 100644 --- a/app/src/main/java/com/frank/ffmpeg/model/LrcInfo.kt +++ b/app/src/main/java/com/frank/ffmpeg/model/LrcInfo.kt @@ -1,8 +1,5 @@ package com.frank.ffmpeg.model -import java.util.ArrayList - - class LrcInfo { var title: String? = null @@ -14,5 +11,5 @@ class LrcInfo { var version: String? = null var offset: Int = 0 - var lrcLineList: ArrayList? = null + var lrcLineList: List? = null } 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 322c975..1acc091 100644 --- a/app/src/main/java/com/frank/ffmpeg/view/LrcView.kt +++ b/app/src/main/java/com/frank/ffmpeg/view/LrcView.kt @@ -23,7 +23,7 @@ class LrcView(context: Context, attr: AttributeSet) : View(context, attr) { private val mPadding = 10 - private val mLrcFontSize = 35 + private val mLrcFontSize = 45 private var mHighLightRow = 0 @@ -48,7 +48,7 @@ class LrcView(context: Context, attr: AttributeSet) : View(context, attr) { annotation class HighLightMode @HighLightMode - private var mode = MODE_HIGH_LIGHT_KARAOKE + private var mode = MODE_HIGH_LIGHT_NORMAL init {