优化歌词解析

This commit is contained in:
lyswhut 2022-03-15 12:53:24 +08:00
parent a95d697860
commit debd1b7c24

View File

@ -19,12 +19,11 @@ public class LyricPlayer {
String lyric = ""; String lyric = "";
String translationLyric = ""; String translationLyric = "";
List<HashMap> lines; List<HashMap> lines;
// HashMap tags = null; HashMap tags;
boolean isPlay = false; boolean isPlay = false;
int curLineNum = 0; int curLineNum = 0;
int maxLine = 0; int maxLine = 0;
int offset = 150; int offset = 150;
boolean isOffered = false;
int performanceTime = 0; int performanceTime = 0;
int startPlayTime = 0; int startPlayTime = 0;
int delay = 0; int delay = 0;
@ -85,6 +84,29 @@ public class LyricPlayer {
return getNow() - this.performanceTime + startPlayTime; 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() { private void initLines() {
String[] linesStr = lyric.split("\r\n|\n|\r"); String[] linesStr = lyric.split("\r\n|\n|\r");
lines = new ArrayList<HashMap>(); lines = new ArrayList<HashMap>();
@ -172,7 +194,7 @@ public class LyricPlayer {
private void init() { private void init() {
if (lyric == null) lyric = ""; if (lyric == null) lyric = "";
if (translationLyric == null) translationLyric = ""; if (translationLyric == null) translationLyric = "";
// initTag(); initTag();
initLines(); initLines();
onSetLyric(lines); onSetLyric(lines);
} }
@ -180,7 +202,6 @@ public class LyricPlayer {
public void pause() { public void pause() {
if (!isPlay) return; if (!isPlay) return;
isPlay = false; isPlay = false;
isOffered = false;
tempPaused = false; tempPaused = false;
stopTimeout(); stopTimeout();
if (curLineNum == maxLine) return; if (curLineNum == maxLine) return;
@ -196,16 +217,17 @@ public class LyricPlayer {
pause(); pause();
isPlay = true; isPlay = true;
performanceTime = getNow(); performanceTime = getNow() - (int) tags.get("offset") - offset;
startPlayTime = curTime; startPlayTime = curTime;
curLineNum = findCurLineNum(curTime) - 1; curLineNum = findCurLineNum(getCurrentTime()) - 1;
refresh(); refresh();
} }
private int findCurLineNum(int curTime, int startIndex) { private int findCurLineNum(int curTime, int startIndex) {
// Log.d("Lyric", "findCurLineNum: " + startIndex); // Log.d("Lyric", "findCurLineNum: " + startIndex);
if (curTime <= 0) return 0;
int length = lines.size(); int length = lines.size();
for (int index = startIndex; index < length; index++) { for (int index = startIndex; index < length; index++) {
if (curTime < (int) ((HashMap)lines.get(index)).get("time")) return index == 0 ? 0 : index - 1; 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; delay = (int) nextLine.get("time") - (int) curLine.get("time") - driftTime;
// Log.d("Lyric", "delay: " + delay + " driftTime: " + driftTime); // Log.d("Lyric", "delay: " + delay + " driftTime: " + driftTime);
if (delay > 0) { if (delay > 0) {
if (!isOffered && delay >= offset) {
delay -= offset;
isOffered = true;
}
if (isPlay) { if (isPlay) {
startTimeout(() -> { startTimeout(() -> {
if (tempPause) { if (tempPause) {
tempPaused = true; tempPaused = true;
return; return;
} }
if (!isPlay) return;
refresh(); refresh();
}, delay); }, delay);
} }