完善歌词
This commit is contained in:
@@ -12,10 +12,12 @@ const tagRegMap = {
|
||||
const timeoutTools = new TimeoutTools()
|
||||
|
||||
module.exports = class LinePlayer {
|
||||
constructor({ lyric = '', offset = 0, onPlay = function() { }, onSetLyric = function() { } } = {}) {
|
||||
constructor({ lyric = '', translationLyric = '', offset = 0, onPlay = function() { }, onSetLyric = function() { } } = {}) {
|
||||
this.lyric = lyric
|
||||
this.translationLyric = translationLyric
|
||||
this.tags = {}
|
||||
this.lines = null
|
||||
this.translationLines = null
|
||||
this.onPlay = onPlay
|
||||
this.onSetLyric = onSetLyric
|
||||
this.isPlay = false
|
||||
@@ -30,6 +32,7 @@ module.exports = class LinePlayer {
|
||||
|
||||
_init() {
|
||||
if (this.lyric == null) this.lyric = ''
|
||||
if (this.translationLyric == null) this.translationLyric = ''
|
||||
this._initTag()
|
||||
this._initLines()
|
||||
this.onSetLyric(this.lines)
|
||||
@@ -44,26 +47,45 @@ module.exports = class LinePlayer {
|
||||
|
||||
_initLines() {
|
||||
this.lines = []
|
||||
this.translationLines = []
|
||||
const lines = this.lyric.split('\n')
|
||||
const linesMap = {}
|
||||
// const translationLines = this.translationLyric.split('\n')
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
const line = lines[i].trim()
|
||||
let result = timeExp.exec(line)
|
||||
if (result) {
|
||||
const text = line.replace(timeExp, '').trim()
|
||||
if (text) {
|
||||
const timeArr = RegExp.$1.split(':')
|
||||
const timeStr = RegExp.$1
|
||||
const timeArr = timeStr.split(':')
|
||||
if (timeArr.length < 3) timeArr.unshift(0)
|
||||
if (timeArr[2].indexOf('.') > -1) {
|
||||
timeArr.push(...timeArr[2].split('.'))
|
||||
timeArr.splice(2, 1)
|
||||
}
|
||||
this.lines.push({
|
||||
linesMap[timeStr] = {
|
||||
time: parseInt(timeArr[0]) * 60 * 60 * 1000 + parseInt(timeArr[1]) * 60 * 1000 + parseInt(timeArr[2]) * 1000 + parseInt(timeArr[3] || 0),
|
||||
text,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const translationLines = this.translationLyric.split('\n')
|
||||
for (let i = 0; i < translationLines.length; i++) {
|
||||
const line = translationLines[i].trim()
|
||||
let result = timeExp.exec(line)
|
||||
if (result) {
|
||||
const text = line.replace(timeExp, '').trim()
|
||||
if (text) {
|
||||
const timeStr = RegExp.$1
|
||||
const targetLine = linesMap[timeStr]
|
||||
if (targetLine) targetLine.translation = text
|
||||
}
|
||||
}
|
||||
}
|
||||
this.lines = Object.values(linesMap)
|
||||
this.lines.sort((a, b) => {
|
||||
return a.time - b.time
|
||||
})
|
||||
@@ -145,9 +167,11 @@ module.exports = class LinePlayer {
|
||||
}
|
||||
}
|
||||
|
||||
setLyric(lyric) {
|
||||
setLyric(lyric, translationLyric) {
|
||||
console.log(translationLyric)
|
||||
if (this.isPlay) this.pause()
|
||||
this.lyric = lyric
|
||||
this.translationLyric = translationLyric
|
||||
this._init()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user