Vue实现歌词滚动效果(lyric-parser)

作者 : 开心源码 本文共1134个字,预计阅读时间需要3分钟 发布时间: 2022-05-13 共316人阅读

Vue实现歌词滚动效果

本例运用了lyric-parser解析歌词插件

npm install lyric-parser -s

用法

 let lyric = new Lyric(lyricStr, handler) function hanlder({lineNum, txt}){   // this hanlder called when lineNum change }

API

//播放歌词play()//暂停歌词stop()//歌词跳转seek(startTime)//切换播放/暂停状态toggelePlay()

使用插件前

image.png

使用插件后

image.png

具体实现步骤

1、获取歌词,并new Lyric对象。

this.currentLyric = new Lyric(lyric, this.lyricHandle)

2、编写歌词回调函数,封装好滚动函数,此处用了better-scroll的scrollToElement。

// 歌词回调lyricHandle({ lineNum, txt }) {    console.log(lineNum)    if (!this.$refs.lyricLine) {        return    }    this.currentLyricNum = lineNum    if(lineNum > 10) {        let lineEl = this.$refs.lyricLine[lineNum - 10]        if (this.$refs.lyricList) {            this.$nextTick(() => {                this.$refs.lyricList.scrollToElement(lineEl, 1000)            })        }    } else {        if (this.$refs.lyricList) {            this.$nextTick(() => {                this.$refs.lyricList.scrollTo(0, 0, 1000)            })        }    }    this.playingLyric = txt;   },

3、根据时间的变化来触发seek()函数,每次调用seek()就会自动调用2中的函数

        // 进度条拖动改变播放进度        onPercentBarChange(percent) {            const currentTime = this.currentSong.duration * percent            this.currentTime = this.$refs.audio.currentTime = currentTime            if (this.currentLyric) {                this.currentLyric.seek(currentTime * 1000)            }            if (!this.playing) {                this.togglePlaying()            }        },

获取歌曲播放时时间可用audio的currentTime。

以上为本人对项目中遇到问题的少量见地,如有错误请指出,感谢您的观看。

说明
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » Vue实现歌词滚动效果(lyric-parser)

发表回复