播放暂停功能

基础样式

v-show="fullScreen"控制播放器全屏展开还是收缩,
currentSong当前播放的歌曲

将不同的逻辑放到不同的文件中,相同的放到一个文件中

获取fullScreen和currentSong,使用useStore api拿数据

  import { useStore } from 'vuex'
  import { computed, watch, ref, nextTick } from 'vue'
 setup() {
      const store = useStore()
      const fullScreen = computed(() => store.state.fullScreen)
      const currentSong = computed(() => store.getters.currentSong)
      }

播放器时全局组件 在app.vue引入注册

<template>
  <m-header></m-header>
  <tab></tab>
  <router-view></router-view>
  <player></player>
</template>

<script>
  import Header from '@/components/header/header'
  import Tab from '@/components/tab/tab'
  import Player from '@/components/player/player'

  export default {
    components: {
      Player,
      MHeader: Header,
      Tab
    }
  }
</script>

歌曲播放

watch api通过currentSong拿到url,赋值给audio,ref api 拿到audio

watch(currentSong, (newSong) => {
        if (!newSong.id || !newSong.url) {
          return
        }
        const audioEl = audioRef.value
        audioEl.src = newSong.url
        audioEl.play()
      })

收缩
通过commit api提交一个mutation

function goBack() {
        store.commit('setFullScreen', false)
      }

播放按钮的暂停与播放

根据不同playing的播放状态求得playicon来改变图标

const playing = computed(() => store.state.playing)
const playIcon = computed(() => {
        return playing.value ? 'icon-pause' : 'icon-play'
      })

绑定点击事件 click=“togglePlay”
修改全局playing状态

function togglePlay() {
        if (!songReady.value) {
          return
        }
        store.commit('setPlayingState', !playing.value)
      }

定义watch函数,看playing数据的变化,操作audio这个dom

 watch(playing, (newPlaying) => {
        if (!songReady.value) {
          return
        }
        const audioEl = audioRef.value
        if (newPlaying) {
          audioEl.play()
          playLyric()
        } else {
          audioEl.pause()
          stopLyric()
        }
      })

audio自然暂停,使用pause同步

function pause() {
        store.commit('setPlayingState', false)
      }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值