当前位置:首页 > VUE

vue实现歌曲循环

2026-01-18 07:44:34VUE

Vue 实现歌曲循环播放

使用 audio 标签和 Vue 数据绑定

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

vue实现歌曲循环

<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 事件切换歌曲:

vue实现歌曲循环

<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

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 实现列表显示的方法 在 Vue 中实现列表显示通常使用 v-for 指令,结合数组或对象数据渲染动态列表。以下是几种常见的实现方式。 基本列表渲染 通过 v-for 指令遍历数组,动态生成列…

vue 实现复选

vue 实现复选

Vue 实现复选功能 在 Vue 中实现复选功能可以通过多种方式完成,以下是常见的几种方法: 使用 v-model 绑定数组 通过 v-model 绑定一个数组,可以自动收集用户选择的复选框值。…

vue实现slider

vue实现slider

Vue 实现 Slider 组件 使用 Vue 实现 Slider 组件可以通过原生开发或借助第三方库。以下是两种常见方法: 原生实现 Slider 创建一个基础的 Slider 组件,通过 v-m…

vue实现swipe

vue实现swipe

Vue实现Swipe功能的方法 使用第三方库(推荐) Vue生态中有多个成熟的轮播/滑动组件库,例如vue-awesome-swiper或swiper/vue。以下是基于swiper/vue的实现示例…

vue实现tree

vue实现tree

Vue 实现 Tree 组件 使用 Vue 实现 Tree 组件可以通过递归组件的方式来实现层级结构展示。以下是一个完整的实现方法: 基础递归组件实现 创建 Tree 组件文件 Tree.vue,使…

vue 实现$(id)

vue 实现$(id)

在 Vue 中实现类似 jQuery 的 $(id) 功能,可以通过以下几种方式操作 DOM 元素: 使用 ref 属性 Vue 推荐使用 ref 属性来直接访问 DOM 元素或组件实例。在模板中为…