修复桌面歌词播放器会导致应用崩溃的问题

This commit is contained in:
lyswhut 2022-01-28 11:22:27 +08:00
parent 448e2a1373
commit c58de24363
2 changed files with 27 additions and 13 deletions

View File

@ -25,7 +25,8 @@ public class LyricPlayer {
int maxLine = 0; int maxLine = 0;
int offset = 150; int offset = 150;
boolean isOffered = false; boolean isOffered = false;
long performanceTime = 0; int performanceTime = 0;
int startPlayTime = 0;
int delay = 0; int delay = 0;
Object tid = null; Object tid = null;
boolean tempPause = false; boolean tempPause = false;
@ -76,12 +77,12 @@ public class LyricPlayer {
tid = null; tid = null;
} }
private long getNow() { private int getNow() {
return System.nanoTime() / 1000000; return (int)(System.nanoTime() / 1000000);
} }
private int getCurrentTime() { private int getCurrentTime() {
return (int)(getNow() - this.performanceTime); return getNow() - this.performanceTime + startPlayTime;
} }
private void initLines() { private void initLines() {
@ -195,21 +196,27 @@ public class LyricPlayer {
pause(); pause();
isPlay = true; isPlay = true;
performanceTime = getNow() - (long)curTime; performanceTime = getNow();
startPlayTime = curTime;
curLineNum = findCurLineNum(curTime) - 1; curLineNum = findCurLineNum(curTime) - 1;
refresh(); refresh();
} }
private int findCurLineNum(int curTime) { private int findCurLineNum(int curTime, int startIndex) {
// Log.d("Lyric", "findCurLineNum: " + startIndex);
int length = lines.size(); int length = lines.size();
for (int index = 0; 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;
} }
return length - 1; return length - 1;
} }
private int findCurLineNum(int curTime) {
return findCurLineNum(curTime, 0);
}
private void handleMaxLine() { private void handleMaxLine() {
this.onPlay(this.curLineNum); this.onPlay(this.curLineNum);
this.pause(); this.pause();
@ -217,9 +224,10 @@ public class LyricPlayer {
private void refresh() { private void refresh() {
if (tempPaused) tempPaused = false; if (tempPaused) tempPaused = false;
// Log.d("Lyric", "refresh: " + curLineNum);
curLineNum++; curLineNum++;
// Log.d("Lyric", "refresh: " + curLineNum);
if (curLineNum >= maxLine) { if (curLineNum >= maxLine) {
handleMaxLine(); handleMaxLine();
return; return;
@ -227,8 +235,8 @@ public class LyricPlayer {
HashMap curLine = lines.get(curLineNum); HashMap curLine = lines.get(curLineNum);
int currentTime = getCurrentTime(); int currentTime = getCurrentTime();
int driftTime = currentTime - (int) curLine.get("time"); int driftTime = currentTime - (int)curLine.get("time");
// Log.d("Lyric", "driftTime: " + driftTime); // Log.d("Lyric", "driftTime: " + driftTime + " time: " + curLine.get("time") + " currentTime: " + currentTime);
if (driftTime >= 0 || curLineNum == 0) { if (driftTime >= 0 || curLineNum == 0) {
HashMap nextLine = lines.get(curLineNum + 1); HashMap nextLine = lines.get(curLineNum + 1);
@ -250,10 +258,16 @@ public class LyricPlayer {
} }
onPlay(curLineNum); onPlay(curLineNum);
return; return;
} else {
int newCurLineNum = this.findCurLineNum(currentTime, curLineNum + 1);
if (newCurLineNum > curLineNum) curLineNum = newCurLineNum - 1;
// Log.d("Lyric", "refresh--: " + curLineNum + " newCurLineNum: " + newCurLineNum);
refresh();
return;
} }
} }
curLineNum = this.findCurLineNum(currentTime) - 1; curLineNum = this.findCurLineNum(currentTime, curLineNum) - 1;
refresh(); refresh();
} }

View File

@ -4,4 +4,4 @@
### 修复 ### 修复
- 修复包名不一致导致的莫名其妙的崩溃问题 - 修复桌面歌词播放器会导致应用崩溃的问题