1.第一次歌词滚动正常

2.当切换歌曲时就会发生歌词在上一首歌词的进度和当前的来回跳动
3.经过分析
- 是因为当我们正常播放时,然后切换歌曲
- 这时请求歌词时没有去停止以前的歌词,以及清除以前的歌词
- 因为只停止以前的歌词播放是不行的
- 因为当切换歌曲时,虽然在歌词没有加载出来不会播放歌词
- 但是我们在因为准备好时也会执行播放歌词的方法
- 所以只停止歌词,他还会再次开始开始以前的歌词播放
watch(currentSong, async (newSong) => {
if (!newSong.url || !newSong.id) {
return
}
stopLyric()
currentLyric.value = null
const lyric = await getLyric(newSong)
store.commit('addSongLyric', {
song: newSong,
lyric
})
if (currentSong.value.lyric !== lyric) {
return
}
currentLyric.value = new Lyric(lyric, handleLyric)
if (songReady.value) {
playLyric()
}
})
function playLyric () {
const currentLyricVal = currentLyric.value
if (currentLyricVal) {
currentLyricVal.seek(currentTime.value * 1000)
}
}
function stopLyric () {
const currentLyricVal = currentLyric.value
if (currentLyricVal) {
currentLyricVal.stop()
}
}
function handleLyric ({ lineNum }) {
currentLineNum.value = lineNum
const scrollComp = lyricScrollRef.value
const listEl = lyricListRef.value
if (!listEl) {
return
}
if (lineNum > 5) {
const lineEl = listEl.children[lineNum - 5]
scrollComp.scroll.scrollToElement(lineEl, 1000)
} else {
scrollComp.scroll.scrollTo(0, 0, 1000)
}
}