当前位置:首页 > VUE

vue实现歌曲循环

2026-01-18 07:44:34VUE

Vue 实现歌曲循环播放

使用 audio 标签和 Vue 数据绑定

在 Vue 中可以通过 audio 标签的 loop 属性实现单曲循环。以下是一个基础实现示例:

<template>
  <div>
    <audio ref="audioPlayer" :src="currentSong.url" @ended="handleEnded" loop></audio>
    <button @click="togglePlay">{{ isPlaying ? '暂停' : '播放' }}</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentSong: {
        url: 'path/to/song.mp3'
      },
      isPlaying: false
    }
  },
  methods: {
    togglePlay() {
      const audio = this.$refs.audioPlayer
      this.isPlaying ? audio.pause() : audio.play()
      this.isPlaying = !this.isPlaying
    },
    handleEnded() {
      // 循环逻辑已通过loop属性实现
    }
  }
}
</script>

实现播放列表循环

如果需要实现播放列表的循环播放,可以通过监听 ended 事件切换歌曲:

<template>
  <div>
    <audio ref="audioPlayer" :src="currentSong.url" @ended="nextSong"></audio>
    <button @click="togglePlay">{{ isPlaying ? '暂停' : '播放' }}</button>
    <button @click="nextSong">下一首</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      songs: [
        { url: 'song1.mp3' },
        { url: 'song2.mp3' },
        { url: 'song3.mp3' }
      ],
      currentIndex: 0,
      isPlaying: false
    }
  },
  computed: {
    currentSong() {
      return this.songs[this.currentIndex]
    }
  },
  methods: {
    togglePlay() {
      const audio = this.$refs.audioPlayer
      this.isPlaying ? audio.pause() : audio.play()
      this.isPlaying = !this.isPlaying
    },
    nextSong() {
      this.currentIndex = (this.currentIndex + 1) % this.songs.length
      if (this.isPlaying) {
        this.$nextTick(() => {
          this.$refs.audioPlayer.play()
        })
      }
    }
  }
}
</script>

添加循环模式切换

可以实现多种循环模式(单曲循环/列表循环/随机播放):

<template>
  <div>
    <audio ref="audioPlayer" :src="currentSong.url" @ended="handleEnded"></audio>
    <button @click="togglePlay">{{ isPlaying ? '暂停' : '播放' }}</button>
    <button @click="changeMode">
      模式: {{ ['列表循环', '单曲循环', '随机播放'][mode] }}
    </button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      songs: [
        { url: 'song1.mp3' },
        { url: 'song2.mp3' },
        { url: 'song3.mp3' }
      ],
      currentIndex: 0,
      isPlaying: false,
      mode: 0 // 0:列表循环 1:单曲循环 2:随机播放
    }
  },
  computed: {
    currentSong() {
      return this.songs[this.currentIndex]
    }
  },
  methods: {
    togglePlay() {
      const audio = this.$refs.audioPlayer
      this.isPlaying ? audio.pause() : audio.play()
      this.isPlaying = !this.isPlaying
    },
    changeMode() {
      this.mode = (this.mode + 1) % 3
    },
    handleEnded() {
      if (this.mode === 1) {
        this.$refs.audioPlayer.play()
        return
      }

      if (this.mode === 2) {
        this.currentIndex = Math.floor(Math.random() * this.songs.length)
      } else {
        this.currentIndex = (this.currentIndex + 1) % this.songs.length
      }

      if (this.isPlaying) {
        this.$nextTick(() => {
          this.$refs.audioPlayer.play()
        })
      }
    }
  }
}
</script>

使用第三方库

如果需要更强大的音频控制功能,可以考虑使用第三方库如 howler.js

vue实现歌曲循环

import { Howl } from 'howler'

export default {
  data() {
    return {
      player: null,
      currentSong: { url: 'song1.mp3' }
    }
  },
  methods: {
    initPlayer() {
      this.player = new Howl({
        src: [this.currentSong.url],
        loop: true,
        autoplay: false
      })
    },
    togglePlay() {
      if (this.player.playing()) {
        this.player.pause()
      } else {
        this.player.play()
      }
    }
  },
  mounted() {
    this.initPlayer()
  }
}

以上方法提供了不同复杂度的歌曲循环实现方案,可根据具体需求选择适合的方式。

标签: 歌曲vue
分享给朋友:

相关文章

vue状态管理怎么实现

vue状态管理怎么实现

Vue 状态管理实现方法 使用 Vuex(官方推荐) Vuex 是 Vue 的官方状态管理库,适合中大型应用。 安装 Vuex: npm install vuex --save 创建 Store 示…

vue实现活动倒计时

vue实现活动倒计时

实现活动倒计时的基本思路 在Vue中实现活动倒计时通常需要获取当前时间与活动结束时间的差值,并将其转换为天、小时、分钟、秒的格式展示。通过定时器每秒更新显示的时间。 定义倒计时数据 在Vue组件的d…

vue 实现jqslidedown

vue 实现jqslidedown

在 Vue 中实现类似 jQuery 的 slideDown 效果,可以通过 Vue 的过渡系统或 CSS 动画结合动态样式绑定来实现。以下是几种实现方式: 使用 Vue Transition 组件…

vue实现机制

vue实现机制

Vue 实现机制的核心原理 Vue.js 的核心实现机制主要基于响应式系统、虚拟 DOM 和组件化设计。以下是关键实现细节: 响应式系统 Vue 通过 Object.defineProperty(V…

vue实现数组

vue实现数组

Vue 实现数组操作的方法 在 Vue 中,数组的操作需要特别注意响应式的问题。Vue 提供了一些方法来确保数组的变动能够触发视图更新。 响应式数组方法 Vue 对以下数组方法进行了封装,确保它们能…

实现vue datepicker

实现vue datepicker

安装依赖 使用 vue-datepicker 或第三方库如 v-calendar 或 vue2-datepicker。以 vue2-datepicker 为例: npm install vue2…