优化歌词播放器

pull/930/merge
lyswhut 2022-03-14 17:06:50 +08:00
parent 0f11731c01
commit b025af45fa
3 changed files with 56 additions and 42 deletions

View File

@ -36,7 +36,7 @@ module.exports = class FontPlayer {
this.curFontNum = 0 this.curFontNum = 0
this.maxFontNum = 0 this.maxFontNum = 0
this._performanceTime = 0 this._performanceTime = 0
this._performanceOffsetTime = 0 this._startTime = 0
this.fontContent = null this.fontContent = null
@ -141,12 +141,12 @@ module.exports = class FontPlayer {
} }
_currentTime() { _currentTime() {
return getNow() - this._performanceTime + this._performanceOffsetTime return getNow() - this._performanceTime + this._startTime
} }
_findcurFontNum(curTime) { _findcurFontNum(curTime, startIndex = 0) {
const length = this.fonts.length const length = this.fonts.length
for (let index = 0; index < length; index++) if (curTime <= this.fonts[index].startTime) return index === 0 ? 0 : index - 1 for (let index = startIndex; index < length; index++) if (curTime < this.fonts[index].startTime) return index == 0 ? 0 : index - 1
return length - 1 return length - 1
} }
@ -195,9 +195,8 @@ module.exports = class FontPlayer {
_refresh() { _refresh() {
this.curFontNum++ this.curFontNum++
// console.log('curFontNum time', this.fonts[this.curFontNum].time) // console.log('curFontNum time', this.fonts[this.curFontNum].time)
if (this.curFontNum === this.maxFontNum) return this._handlePlayMaxFontNum() if (this.curFontNum >= this.maxFontNum) return this._handlePlayMaxFontNum()
let curFont = this.fonts[this.curFontNum] let curFont = this.fonts[this.curFontNum]
let nextFont = this.fonts[this.curFontNum + 1]
// console.log(curFont, nextFont, this.curFontNum, this.maxFontNum) // console.log(curFont, nextFont, this.curFontNum, this.maxFontNum)
const currentTime = this._currentTime() const currentTime = this._currentTime()
// console.log(curFont.text) // console.log(curFont.text)
@ -206,27 +205,38 @@ module.exports = class FontPlayer {
// console.log(currentTime, driftTime) // console.log(currentTime, driftTime)
if (driftTime >= 0 || this.curFontNum == 0) { if (driftTime >= 0 || this.curFontNum == 0) {
let nextFont = this.fonts[this.curFontNum + 1]
this.delay = nextFont.startTime - curFont.startTime - driftTime this.delay = nextFont.startTime - curFont.startTime - driftTime
if (this.delay > 0) { if (this.delay > 0) {
this._handlePlayFont(curFont, driftTime) if (this.isPlay) {
this.timeoutTools.start(() => { this.timeoutTools.start(() => {
if (!this.isPlay) return if (!this.isPlay) return
this._refresh() this._refresh()
}, this.delay) }, this.delay)
}
this._handlePlayFont(curFont, driftTime)
return
} else {
let newCurLineNum = this._findcurFontNum(currentTime, this.curFontNum + 1)
if (newCurLineNum > this.curFontNum) this.curFontNum = newCurLineNum - 1
for (let i = 0; i <= this.curFontNum; i++) this._handlePlayFont(this.fonts[i], 0, true)
this._refresh()
return return
} }
} else if (this.curFontNum == 0) { } else if (this.curFontNum == 0) {
this.curFontNum-- this.curFontNum--
if (this.isPlay) {
this.waitPlayTimeout.start(() => { this.waitPlayTimeout.start(() => {
if (!this.isPlay) return if (!this.isPlay) return
this._refresh() this._refresh()
}, -driftTime) }, -driftTime)
}
return return
} }
this.curFontNum = this._findcurFontNum(currentTime) this.curFontNum = this._findcurFontNum(currentTime, this.curFontNum) - 1
for (let i = 0; i < this.curFontNum; i++) this._handlePlayFont(this.fonts[i], 0, true) for (let i = 0; i <= this.curFontNum; i++) this._handlePlayFont(this.fonts[i], 0, true)
this.curFontNum-- // this.curFontNum--
this._refresh() this._refresh()
} }
@ -237,14 +247,11 @@ module.exports = class FontPlayer {
if (this.isLineMode) return this._handlePlayLine(true) if (this.isLineMode) return this._handlePlayLine(true)
this.isPlay = true this.isPlay = true
this._performanceTime = getNow() - curTime this._performanceTime = getNow()
this._performanceOffsetTime = 0 this._startTime = curTime
if (this._performanceTime < 0) {
this._performanceOffsetTime = -this._performanceTime
this._performanceTime = 0
}
this.curFontNum = this._findcurFontNum(curTime) this.curFontNum = this._findcurFontNum(curTime)
for (let i = this.curFontNum; i > -1; i--) { for (let i = this.curFontNum; i > -1; i--) {
this._handlePlayFont(this.fonts[i], 0, true) this._handlePlayFont(this.fonts[i], 0, true)
} }

View File

@ -7,7 +7,7 @@ module.exports = class Lyric {
constructor({ constructor({
lyric = '', lyric = '',
translationLyric = '', translationLyric = '',
offset = 150, offset = 0,
lineClassName = '', lineClassName = '',
fontClassName = 'font', fontClassName = 'font',
translationClassName = 'translation', translationClassName = 'translation',
@ -166,5 +166,6 @@ module.exports = class Lyric {
this.lyric = lyric this.lyric = lyric
this.translationLyric = translationLyric this.translationLyric = translationLyric
this._init() this._init()
this.linePlayer.offset = this.isLineMode ? this.offset + 150 : this.offset
} }
} }

View File

@ -26,7 +26,7 @@ module.exports = class LinePlayer {
this.offset = offset this.offset = offset
this.isOffseted = false this.isOffseted = false
this._performanceTime = 0 this._performanceTime = 0
this._performanceOffsetTime = 0 this._startTime = 0
this._init() this._init()
} }
@ -93,12 +93,12 @@ module.exports = class LinePlayer {
} }
_currentTime() { _currentTime() {
return getNow() - this._performanceTime + this._performanceOffsetTime return getNow() - this._performanceTime + this._startTime
} }
_findCurLineNum(curTime) { _findCurLineNum(curTime, startIndex = 0) {
const length = this.lines.length const length = this.lines.length
for (let index = 0; 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
} }
@ -110,30 +110,40 @@ module.exports = class LinePlayer {
_refresh() { _refresh() {
this.curLineNum++ this.curLineNum++
// console.log('curLineNum time', this.lines[this.curLineNum].time) // console.log('curLineNum time', this.lines[this.curLineNum].time)
if (this.curLineNum >= this.maxLine) return this._handleMaxLine()
let curLine = this.lines[this.curLineNum] let curLine = this.lines[this.curLineNum]
let nextLine = this.lines[this.curLineNum + 1]
const currentTime = this._currentTime() const currentTime = this._currentTime()
const driftTime = currentTime - curLine.time const driftTime = currentTime - curLine.time
if (driftTime >= 0 || this.curLineNum === 0) { if (driftTime >= 0 || this.curLineNum === 0) {
if (this.curLineNum === this.maxLine) return this._handleMaxLine() let nextLine = this.lines[this.curLineNum + 1]
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) { if (!this.isOffseted && this.delay >= this.offset) {
this._performanceOffsetTime += this.offset this._startTime += this.offset
this.delay -= this.offset this.delay -= this.offset
this.isOffseted = true this.isOffseted = true
} }
if (this.isPlay) {
timeoutTools.start(() => { timeoutTools.start(() => {
if (!this.isPlay) return if (!this.isPlay) return
this._refresh() this._refresh()
}, this.delay) }, this.delay)
}
this.onPlay(this.curLineNum, curLine.text, currentTime) this.onPlay(this.curLineNum, curLine.text, currentTime)
return return
} else {
let newCurLineNum = this._findCurLineNum(currentTime, this.curLineNum + 1)
if (newCurLineNum > this.curLineNum) this.curLineNum = newCurLineNum - 1
this._refresh()
return
} }
} }
this.curLineNum = this._findCurLineNum(currentTime) - 1 this.curLineNum = this._findCurLineNum(currentTime, this.curLineNum) - 1
this._refresh() this._refresh()
} }
@ -142,12 +152,8 @@ module.exports = class LinePlayer {
this.pause() this.pause()
this.isPlay = true this.isPlay = true
this._performanceOffsetTime = 0 this._performanceTime = getNow()
this._performanceTime = getNow() - curTime this._startTime = curTime
if (this._performanceTime < 0) {
this._performanceOffsetTime = -this._performanceTime
this._performanceTime = 0
}
this.curLineNum = this._findCurLineNum(curTime) - 1 this.curLineNum = this._findCurLineNum(curTime) - 1