From debd1b7c24f5be845ad893ef5d7bad7a2a36f6e8 Mon Sep 17 00:00:00 2001 From: lyswhut Date: Tue, 15 Mar 2022 12:53:24 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=AD=8C=E8=AF=8D=E8=A7=A3?= =?UTF-8?q?=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../music/mobile/lyric/LyricPlayer.java | 39 ++++++++++++++----- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/android/app/src/main/java/cn/toside/music/mobile/lyric/LyricPlayer.java b/android/app/src/main/java/cn/toside/music/mobile/lyric/LyricPlayer.java index bac1ff4..e35cf22 100644 --- a/android/app/src/main/java/cn/toside/music/mobile/lyric/LyricPlayer.java +++ b/android/app/src/main/java/cn/toside/music/mobile/lyric/LyricPlayer.java @@ -19,12 +19,11 @@ public class LyricPlayer { String lyric = ""; String translationLyric = ""; List lines; -// HashMap tags = null; + HashMap tags; boolean isPlay = false; int curLineNum = 0; int maxLine = 0; int offset = 150; - boolean isOffered = false; int performanceTime = 0; int startPlayTime = 0; int delay = 0; @@ -85,6 +84,29 @@ public class LyricPlayer { return getNow() - this.performanceTime + startPlayTime; } + private void initTag() { + tags = new HashMap(); + Matcher matcher = Pattern.compile("\\[(ti|ar|al|offset|by):\\s*(\\S+(?:\\s+\\S+)*)\\s*\\]").matcher(this.lyric); + while (matcher.find()) { + String key = matcher.group(1); + if (key == null) continue; + String val = matcher.group(2); + if (val == null) val = ""; + tags.put(key, val); + } + + String offsetStr = (String) tags.get("offset"); + if (offsetStr == null || offsetStr.equals("")) { + tags.put("offset", 0); + } else { + int offset = 0; + try { + offset = Integer.parseInt(offsetStr); + } catch (Exception err) {} + tags.put("offset", offset); + } + } + private void initLines() { String[] linesStr = lyric.split("\r\n|\n|\r"); lines = new ArrayList(); @@ -172,7 +194,7 @@ public class LyricPlayer { private void init() { if (lyric == null) lyric = ""; if (translationLyric == null) translationLyric = ""; -// initTag(); + initTag(); initLines(); onSetLyric(lines); } @@ -180,7 +202,6 @@ public class LyricPlayer { public void pause() { if (!isPlay) return; isPlay = false; - isOffered = false; tempPaused = false; stopTimeout(); if (curLineNum == maxLine) return; @@ -196,16 +217,17 @@ public class LyricPlayer { pause(); isPlay = true; - performanceTime = getNow(); + performanceTime = getNow() - (int) tags.get("offset") - offset; startPlayTime = curTime; - curLineNum = findCurLineNum(curTime) - 1; + curLineNum = findCurLineNum(getCurrentTime()) - 1; refresh(); } private int findCurLineNum(int curTime, int startIndex) { // Log.d("Lyric", "findCurLineNum: " + startIndex); + if (curTime <= 0) return 0; int length = lines.size(); for (int index = startIndex; index < length; index++) { if (curTime < (int) ((HashMap)lines.get(index)).get("time")) return index == 0 ? 0 : index - 1; @@ -243,16 +265,13 @@ public class LyricPlayer { delay = (int) nextLine.get("time") - (int) curLine.get("time") - driftTime; // Log.d("Lyric", "delay: " + delay + " driftTime: " + driftTime); if (delay > 0) { - if (!isOffered && delay >= offset) { - delay -= offset; - isOffered = true; - } if (isPlay) { startTimeout(() -> { if (tempPause) { tempPaused = true; return; } + if (!isPlay) return; refresh(); }, delay); }