优化歌词偏移时间计算

pull/930/merge
lyswhut 2022-03-15 10:23:47 +08:00
parent 04858cc42c
commit 6779fd7ae5
1 changed files with 3 additions and 11 deletions

View File

@ -24,10 +24,8 @@ module.exports = class LinePlayer {
this.curLineNum = 0 this.curLineNum = 0
this.maxLine = 0 this.maxLine = 0
this.offset = offset this.offset = offset
this.isOffseted = false
this._performanceTime = 0 this._performanceTime = 0
this._startTime = 0 this._startTime = 0
this._offset = 0
this._init() this._init()
} }
@ -105,6 +103,7 @@ module.exports = class LinePlayer {
} }
_findCurLineNum(curTime, startIndex = 0) { _findCurLineNum(curTime, startIndex = 0) {
if (curTime <= 0) return 0
const length = this.lines.length const length = this.lines.length
for (let index = startIndex; index < length; index++) if (curTime <= this.lines[index].time) return index === 0 ? 0 : index - 1 for (let index = startIndex; index < length; index++) if (curTime <= this.lines[index].time) return index === 0 ? 0 : index - 1
return length - 1 return length - 1
@ -130,11 +129,6 @@ module.exports = class LinePlayer {
this.delay = nextLine.time - curLine.time - driftTime this.delay = nextLine.time - curLine.time - driftTime
if (this.delay > 0) { if (this.delay > 0) {
if (!this.isOffseted && this.delay >= this._offset) {
this._startTime += this._offset
this.delay -= this._offset
this.isOffseted = true
}
if (this.isPlay) { if (this.isPlay) {
timeoutTools.start(() => { timeoutTools.start(() => {
if (!this.isPlay) return if (!this.isPlay) return
@ -160,11 +154,10 @@ module.exports = class LinePlayer {
this.pause() this.pause()
this.isPlay = true this.isPlay = true
this._performanceTime = getNow() this._performanceTime = getNow() - parseInt(this.tags.offset + this.offset)
this._startTime = curTime this._startTime = curTime
this._offset = this.tags.offset + this.offset
this.curLineNum = this._findCurLineNum(curTime) - 1 this.curLineNum = this._findCurLineNum(this._currentTime()) - 1
this._refresh() this._refresh()
} }
@ -172,7 +165,6 @@ module.exports = class LinePlayer {
pause() { pause() {
if (!this.isPlay) return if (!this.isPlay) return
this.isPlay = false this.isPlay = false
this.isOffseted = false
timeoutTools.clear() timeoutTools.clear()
if (this.curLineNum === this.maxLine) return if (this.curLineNum === this.maxLine) return
const currentTime = this._currentTime() const currentTime = this._currentTime()