Vue音乐项目笔记(三)

时间:2021-11-03 14:40:01

1. 音乐播放前进后退的实现   https://blog.csdn.net/weixin_40814356/article/details/80379606

Vue音乐项目笔记(三)

Vue音乐项目笔记(三)

2. 音乐进度条实现(单独一个组件) https://blog.csdn.net/weixin_40814356/article/details/80387804

思想: a. 播放进度条样式  定义高但是不定义宽度。 父组件computed: 当音乐不断变化的时候 它与总时间有个不断变化的百分比

子组件接收的时候 watch它的变化 不断去修改progress和小球的宽度

b. 实现拖动效果

给progress-bar定义触摸事件

Vue音乐项目笔记(三)

在created中创建一个this.touch={}对象,用来保存函数中的一些数据

初始化touch事件 记录第一次触摸的位置,以及此时小球偏移的位置   在progressTouchMove方法中,记录触摸的距离(即拖动的距离),得出当前偏移距离(记录的小球偏移距离加上差值) 结束的时候把初始化置为false

然后实现拖动到指定位置,歌曲播放指定位置的功能    touch结束的时候,向父组件触发一个事件,获取bar的宽度,然后用progress的宽度/bar的宽度,得到百分比 通过$emit传递出去  父组件接收到事件 改变audio的播放点

Vue音乐项目笔记(三)

Vue音乐项目笔记(三)

Vue音乐项目笔记(三)

最最后:实现点击的功能

Vue音乐项目笔记(三)

c. 音乐环形进度条的实现

逻辑的实现:

Vue音乐项目笔记(三)

Vue音乐项目笔记(三)

3.如何获取歌词的数据,并解析jsonp的格式为json的格式

https://blog.csdn.net/weixin_40814356/article/details/80401989

4. 歌词左右滑动的实现  https://blog.csdn.net/weixin_40814356/article/details/80417580

给中间加一个touch事件 用一个currentShow保存歌词显示和隐藏的状态:是唱片页还是歌词页

touchstart的时候维护几个状态:记录x轴和y轴的坐标

touchmove:

 middleTouchMove (e) {

       if (!this.touch.init) {

         return

       }

       const touch = e.touches[0]

       const deltaX = touch.pageX - this.touch.startX

       const deltaY = touch.pageY - this.touch.startY

       // 为什么维护纵轴,当纵轴大于横轴的偏移的时候,就不应该移动

       if (Math.abs(deltaY) > Math.abs(deltaX)) {

         return

       }

       // 首先记录歌词的起始位置

       const left = this.currentShow === 'cd' ? 0 : -window.innerWidth

       // 最终就两种状态,left的值有两种状态,如果是cd,就是0

       const offsetWidth = Math.min(0, Math.max(-window.innerWidth, left + deltaX))

       // 假如是左滑,那么dalte是负的

       this.touch.percent = Math.abs(offsetWidth / window.innerWidth)

       //这个percent是维护偏移的距离,>0.1和<0.9

       this.$refs.lyricList.$el.style[transform] = `translate3d(${offsetWidth}px, 0, 0)`

       // lyricList是一个scroll组件,是一个vue组件,通过$el可以获取dom

       this.$refs.lyricList.$el.style[transitionDuration] = 0

       this.$refs.middleL.style.opacity = 1 - this.touch.percent

     },

touchend:

 middleTouchEnd () {

       let offsetWidth

       let opacity

       // 定义offset和opacity

       if (this.currentShow === 'cd') {

         // 如果在cd的情况下

         if (this.touch.percent > 0.1) {

           // 当活动距离超出0.1,做如下操作

           offsetWidth = -window.innerWidth

           opacity = 0

           this.currentShow = 'lyric'

         } else {

           offsetWidth = 0

           opacity = 1

           // 否则回复状态

         }

       } else {

         if (this.touch.percent < 0.9) {

           // 当活动距离小于0.9,做如下操作

           offsetWidth = 0

           opacity = 1

           this.currentShow = 'cd'

         } else {

           offsetWidth = -window.innerWidth

           opacity = 0

         }

       }

       const time = 300

       this.$refs.lyricList.$el.style[transform] = `translate3d(${offsetWidth}px, 0, 0)`

       // 改变他的位置

       this.$refs.lyricList.$el.style[transitionDuration] = `${time}ms`

       // 过渡的时间,在move的时候要清零

       this.$refs.middleL.style.opacity = opacity

       this.$refs.middleL.style[transitionDuration] = `${time}ms`

     },

解决歌词不断跳动:实际上就是清空lyric中的timer计时器

**if (this.currentLyric) {

        this.currentLyric.stop()

      }**

解决歌词和音乐同步播放:

this.setPlayingState(!this.playing)

      if (this.currentLyric) {

        this.currentLyric.togglePlay()

      }

解决循环播放不会到一开始的问题,在loop的代码如下:

if (this.currentLyric) {

          this.currentLyric.seek(0)

        }

拖动bar的时候,歌词跟着滚动:

Vue音乐项目笔记(三)

播放页歌词的实现:

添加dom

  1. <div class="playing-lyric-wrapper">
  2. <div class="playing-lyric">{{playingLyric}}</div>
  3. </div>

维护一个playLric

Vue音乐项目笔记(三)

然后:在lyric回调的时候设置playingLric

Vue音乐项目笔记(三)