当前位置:首页 > VUE

vue 实现音乐

2026-03-07 12:52:51VUE

Vue 实现音乐播放功能

使用 Vue 实现音乐播放功能可以通过以下步骤完成,结合 HTML5 的 <audio> 元素和 Vue 的数据绑定特性。

创建 Vue 项目

确保已安装 Vue CLI,通过以下命令创建新项目:

vue create music-player
cd music-player

安装依赖

如果需要使用第三方库(如 howler.jsvue-audio-visual),可以通过 npm 安装:

vue 实现音乐

npm install howler.js

基本音乐播放器实现

在 Vue 组件中,使用 <audio> 标签和 Vue 的数据绑定控制播放状态。

<template>
  <div>
    <audio ref="audioPlayer" :src="currentSong.url" @timeupdate="updateTime"></audio>
    <button @click="togglePlay">{{ isPlaying ? '暂停' : '播放' }}</button>
    <input type="range" v-model="currentTime" max="100" @input="seek">
    <span>{{ formatTime(currentTime) }}</span>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentSong: {
        url: 'path/to/your/music.mp3',
        title: '歌曲名称'
      },
      isPlaying: false,
      currentTime: 0,
      duration: 0
    };
  },
  methods: {
    togglePlay() {
      const audio = this.$refs.audioPlayer;
      if (this.isPlaying) {
        audio.pause();
      } else {
        audio.play();
      }
      this.isPlaying = !this.isPlaying;
    },
    updateTime(e) {
      const audio = e.target;
      this.currentTime = (audio.currentTime / audio.duration) * 100;
      this.duration = audio.duration;
    },
    seek(e) {
      const audio = this.$refs.audioPlayer;
      audio.currentTime = (e.target.value / 100) * audio.duration;
    },
    formatTime(percent) {
      const time = (percent / 100) * this.duration;
      const minutes = Math.floor(time / 60);
      const seconds = Math.floor(time % 60);
      return `${minutes}:${seconds < 10 ? '0' : ''}${seconds}`;
    }
  }
};
</script>

使用第三方库(Howler.js)

如果需要更高级的功能(如音频可视化),可以使用 howler.js

vue 实现音乐

<template>
  <div>
    <button @click="togglePlay">{{ isPlaying ? '暂停' : '播放' }}</button>
  </div>
</template>

<script>
import { Howl } from 'howler';

export default {
  data() {
    return {
      sound: null,
      isPlaying: false
    };
  },
  created() {
    this.sound = new Howl({
      src: ['path/to/your/music.mp3'],
      onend: () => {
        this.isPlaying = false;
      }
    });
  },
  methods: {
    togglePlay() {
      if (this.isPlaying) {
        this.sound.pause();
      } else {
        this.sound.play();
      }
      this.isPlaying = !this.isPlaying;
    }
  }
};
</script>

实现播放列表

扩展功能以支持多首歌曲切换。

<template>
  <div>
    <audio ref="audioPlayer" :src="currentSong.url" @ended="nextSong"></audio>
    <button @click="togglePlay">{{ isPlaying ? '暂停' : '播放' }}</button>
    <button @click="prevSong">上一首</button>
    <button @click="nextSong">下一首</button>
    <ul>
      <li v-for="(song, index) in playlist" :key="index" @click="playSong(index)">
        {{ song.title }}
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      playlist: [
        { url: 'path/to/song1.mp3', title: '歌曲1' },
        { url: 'path/to/song2.mp3', title: '歌曲2' }
      ],
      currentSongIndex: 0,
      isPlaying: false
    };
  },
  computed: {
    currentSong() {
      return this.playlist[this.currentSongIndex];
    }
  },
  methods: {
    togglePlay() {
      const audio = this.$refs.audioPlayer;
      if (this.isPlaying) {
        audio.pause();
      } else {
        audio.play();
      }
      this.isPlaying = !this.isPlaying;
    },
    playSong(index) {
      this.currentSongIndex = index;
      this.isPlaying = true;
      this.$nextTick(() => {
        this.$refs.audioPlayer.play();
      });
    },
    prevSong() {
      this.currentSongIndex = (this.currentSongIndex - 1 + this.playlist.length) % this.playlist.length;
      this.playSong(this.currentSongIndex);
    },
    nextSong() {
      this.currentSongIndex = (this.currentSongIndex + 1) % this.playlist.length;
      this.playSong(this.currentSongIndex);
    }
  }
};
</script>

音频可视化

使用 vue-audio-visual 实现音频频谱可视化。

npm install vue-audio-visual
<template>
  <div>
    <audio ref="audioPlayer" :src="currentSong.url"></audio>
    <button @click="togglePlay">{{ isPlaying ? '暂停' : '播放' }}</button>
    <vue-audio-visual
      :audio-element="$refs.audioPlayer"
      :width="300"
      :height="100"
      :bar-width="2"
      :gap="1"
      :fft-size="64"
    />
  </div>
</template>

<script>
import VueAudioVisual from 'vue-audio-visual';

export default {
  components: { VueAudioVisual },
  data() {
    return {
      currentSong: { url: 'path/to/your/music.mp3' },
      isPlaying: false
    };
  },
  methods: {
    togglePlay() {
      const audio = this.$refs.audioPlayer;
      if (this.isPlaying) {
        audio.pause();
      } else {
        audio.play();
      }
      this.isPlaying = !this.isPlaying;
    }
  }
};
</script>

注意事项

  • 音乐文件路径需正确,建议放在 public 文件夹或通过 URL 引用。
  • 跨域问题需确保服务器配置正确(如 CORS)。
  • 移动端需注意浏览器的自动播放策略(通常需用户交互后触发播放)。

标签: 音乐vue
分享给朋友:

相关文章

vue实现erp

vue实现erp

Vue实现ERP系统的关键步骤 技术选型与架构设计 采用Vue 3 + TypeScript构建前端,搭配Pinia状态管理,Element Plus或Ant Design Vue作为UI框架。后端可…

vue实现RTMP

vue实现RTMP

Vue 中实现 RTMP 流播放 RTMP(Real-Time Messaging Protocol)是一种用于实时音视频流传输的协议。在 Vue 中实现 RTMP 播放通常需要借助第三方库或播放器。…

vue实现分栏

vue实现分栏

Vue实现分栏的常见方法 使用CSS Flexbox布局 通过Flexbox可以快速实现分栏效果,适合大多数响应式布局场景 <template> <div class="con…

vue 实现上传

vue 实现上传

Vue 实现文件上传 使用 Vue 实现文件上传可以通过原生 HTML 的 <input type="file"> 或第三方库(如 axios、vue-upload-component)完…

vue 实现图片

vue 实现图片

Vue 实现图片加载与展示的方法 在 Vue 中实现图片加载和展示可以通过多种方式完成,以下是一些常见的方法: 使用 v-bind 绑定图片路径 通过 v-bind(或简写为 :)动态绑定图片的…

vue实现匹配显示

vue实现匹配显示

Vue实现匹配显示的方法 使用v-if或v-show指令 通过条件渲染指令v-if或v-show,可以根据条件匹配显示不同的内容。v-if会完全销毁或重建DOM元素,而v-show仅切换CSS的dis…