Skip to content

Commit

Permalink
修复桌面歌词播放器会导致应用崩溃的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
lyswhut committed Jan 28, 2022
1 parent 448e2a1 commit c58de24
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public class LyricPlayer {
int maxLine = 0;
int offset = 150;
boolean isOffered = false;
long performanceTime = 0;
int performanceTime = 0;
int startPlayTime = 0;
int delay = 0;
Object tid = null;
boolean tempPause = false;
Expand Down Expand Up @@ -76,12 +77,12 @@ private void stopTimeout() {
tid = null;
}

private long getNow() {
return System.nanoTime() / 1000000;
private int getNow() {
return (int)(System.nanoTime() / 1000000);
}

private int getCurrentTime() {
return (int)(getNow() - this.performanceTime);
return getNow() - this.performanceTime + startPlayTime;
}

private void initLines() {
Expand Down Expand Up @@ -195,40 +196,47 @@ public void play(int curTime) {
pause();
isPlay = true;

performanceTime = getNow() - (long)curTime;
performanceTime = getNow();
startPlayTime = curTime;

curLineNum = findCurLineNum(curTime) - 1;

refresh();
}

private int findCurLineNum(int curTime) {
private int findCurLineNum(int curTime, int startIndex) {
// Log.d("Lyric", "findCurLineNum: " + startIndex);
int length = lines.size();
for (int index = 0; index < length; index++) {
if (curTime <= (int) ((HashMap)lines.get(index)).get("time")) return index == 0 ? 0 : index - 1;
for (int index = startIndex; index < length; index++) {
if (curTime < (int) ((HashMap)lines.get(index)).get("time")) return index == 0 ? 0 : index - 1;
}
return length - 1;
}

private int findCurLineNum(int curTime) {
return findCurLineNum(curTime, 0);
}

private void handleMaxLine() {
this.onPlay(this.curLineNum);
this.pause();
}

private void refresh() {
if (tempPaused) tempPaused = false;
// Log.d("Lyric", "refresh: " + curLineNum);

curLineNum++;
// Log.d("Lyric", "refresh: " + curLineNum);

if (curLineNum >= maxLine) {
handleMaxLine();
return;
}
HashMap curLine = lines.get(curLineNum);

int currentTime = getCurrentTime();
int driftTime = currentTime - (int) curLine.get("time");
// Log.d("Lyric", "driftTime: " + driftTime);
int driftTime = currentTime - (int)curLine.get("time");
// Log.d("Lyric", "driftTime: " + driftTime + " time: " + curLine.get("time") + " currentTime: " + currentTime);

if (driftTime >= 0 || curLineNum == 0) {
HashMap nextLine = lines.get(curLineNum + 1);
Expand All @@ -250,10 +258,16 @@ private void refresh() {
}
onPlay(curLineNum);
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();
}

Expand Down
2 changes: 1 addition & 1 deletion publish/changeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

### 修复

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

0 comments on commit c58de24

Please sign in to comment.