using work thread to parse lrc, and callback to main thread

dev
xufuji456 3 years ago
parent cdb33387e2
commit 174a13a3c6
  1. 32
      app/src/main/java/com/frank/ffmpeg/activity/AudioPlayActivity.kt
  2. 2
      app/src/main/java/com/frank/ffmpeg/model/AudioBean.kt
  3. 5
      app/src/main/java/com/frank/ffmpeg/tool/LrcParser.kt
  4. 2
      app/src/main/java/com/frank/ffmpeg/view/LrcView.kt

@ -15,12 +15,14 @@ import com.frank.ffmpeg.R
import com.frank.ffmpeg.handler.FFmpegHandler import com.frank.ffmpeg.handler.FFmpegHandler
import com.frank.ffmpeg.handler.FFmpegHandler.MSG_FINISH import com.frank.ffmpeg.handler.FFmpegHandler.MSG_FINISH
import com.frank.ffmpeg.listener.OnLrcListener import com.frank.ffmpeg.listener.OnLrcListener
import com.frank.ffmpeg.model.AudioBean
import com.frank.ffmpeg.model.MediaBean import com.frank.ffmpeg.model.MediaBean
import com.frank.ffmpeg.util.FFmpegUtil import com.frank.ffmpeg.util.FFmpegUtil
import com.frank.ffmpeg.util.TimeUtil import com.frank.ffmpeg.util.TimeUtil
import com.frank.ffmpeg.model.LrcLine import com.frank.ffmpeg.model.LrcLine
import com.frank.ffmpeg.tool.LrcLineTool import com.frank.ffmpeg.tool.LrcLineTool
import com.frank.ffmpeg.tool.LrcParser import com.frank.ffmpeg.tool.LrcParser
import com.frank.ffmpeg.util.ThreadPoolUtil
import com.frank.ffmpeg.view.LrcView import com.frank.ffmpeg.view.LrcView
import java.io.File import java.io.File
@ -63,9 +65,10 @@ class AudioPlayActivity : AppCompatActivity() {
MSG_FINISH -> { MSG_FINISH -> {
if (msg.obj == null) return if (msg.obj == null) return
val result = msg.obj as MediaBean val result = msg.obj as MediaBean
txtTitle?.text = result.audioBean?.title val audioBean = result.audioBean
txtArtist?.text = result.audioBean?.artist txtTitle?.text = audioBean?.title
val lyrics = result.audioBean?.lyrics txtArtist?.text = audioBean?.artist
val lyrics = audioBean?.lyrics
if (lyrics != null) { if (lyrics != null) {
val lrcList = arrayListOf<LrcLine>() val lrcList = arrayListOf<LrcLine>()
for (i in lyrics.indices) { for (i in lyrics.indices) {
@ -75,6 +78,8 @@ class AudioPlayActivity : AppCompatActivity() {
} }
LrcLineTool.sortLyrics(lrcList) LrcLineTool.sortLyrics(lrcList)
lrcView?.setLrc(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()) { if (!lrcPath.isNullOrEmpty() && File(lrcPath).exists()) {
// should parsing in work thread // should parsing in work thread
val lrcParser = LrcParser() ThreadPoolUtil.executeSingleThreadPool(Runnable {
val lrcInfo = lrcParser.readLrc(lrcPath) val lrcParser = LrcParser()
Log.e(TAG, "title=${lrcInfo?.title},album=${lrcInfo?.album},artist=${lrcInfo?.artist}") val lrcInfo = lrcParser.readLrc(lrcPath)
if (lrcInfo?.lrcLineList != null) { Log.e(TAG, "title=${lrcInfo?.title},album=${lrcInfo?.album},artist=${lrcInfo?.artist}")
lrcView?.setLrc(lrcInfo.lrcLineList!!) val mediaBean = MediaBean()
} val audioBean = AudioBean()
txtTitle?.text = lrcInfo?.title audioBean.title = lrcInfo?.title
txtArtist?.text = lrcInfo?.artist audioBean.album = lrcInfo?.album
audioBean.artist = lrcInfo?.artist
audioBean.lrcLineList = lrcInfo?.lrcLineList
mediaBean.audioBean = audioBean
mHandler.obtainMessage(MSG_FINISH, mediaBean).sendToTarget()
})
} else { } else {
val ffmpegHandler = FFmpegHandler(mHandler) val ffmpegHandler = FFmpegHandler(mHandler)
val commandLine = FFmpegUtil.probeFormat(path) val commandLine = FFmpegUtil.probeFormat(path)

@ -35,4 +35,6 @@ class AudioBean {
var lyrics: List<String>? = null var lyrics: List<String>? = null
var lrcLineList: List<LrcLine>? = null
} }

@ -129,10 +129,12 @@ class LrcParser {
var currentTime: Long = 0 var currentTime: Long = 0
while (matcher.find()) { while (matcher.find()) {
val groupCount = matcher.groupCount() val groupCount = matcher.groupCount()
var currentTimeStr: String? = ""
for (index in 0 until groupCount) { for (index in 0 until groupCount) {
val timeStr = matcher.group(index) val timeStr = matcher.group(index)
if (index == 0) { 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) val content = pattern.split(str)
@ -141,6 +143,7 @@ class LrcParser {
currentContent = content[content.size - 1] currentContent = content[content.size - 1]
} }
val lrcLine = LrcLine() val lrcLine = LrcLine()
lrcLine.timeString = currentTimeStr
lrcLine.startTime = currentTime lrcLine.startTime = currentTime
lrcLine.endTime = currentTime + 10 * 1000 lrcLine.endTime = currentTime + 10 * 1000
lrcLine.content = currentContent lrcLine.content = currentContent

@ -243,7 +243,7 @@ class LrcView(context: Context, attr: AttributeSet) : View(context, attr) {
companion object { companion object {
private const val mSeekLineTextSize = 15 private const val mSeekLineTextSize = 25
private const val mSeekLineColor = Color.RED private const val mSeekLineColor = Color.RED

Loading…
Cancel
Save