当前位置:首页 > VUE

vue实现歌曲切换

2026-02-17 09:08:28VUE

Vue 实现歌曲切换功能

数据准备

在 Vue 组件中定义一个歌曲列表数组,包含歌曲名称、歌手、音频文件路径等信息:

data() {
  return {
    songs: [
      { id: 1, title: '歌曲1', artist: '歌手1', src: '/path/to/song1.mp3' },
      { id: 2, title: '歌曲2', artist: '歌手2', src: '/path/to/song2.mp3' },
      { id: 3, title: '歌曲3', artist: '歌手3', src: '/path/to/song3.mp3' }
    ],
    currentSongIndex: 0,
    audioPlayer: null
  }
}

音频播放器初始化

mounted 生命周期钩子中初始化音频播放器对象:

mounted() {
  this.audioPlayer = new Audio(this.songs[this.currentSongIndex].src)
}

歌曲切换逻辑

实现切换到指定索引歌曲的方法:

methods: {
  playSong(index) {
    if (index >= 0 && index < this.songs.length) {
      this.currentSongIndex = index
      this.audioPlayer.src = this.songs[index].src
      this.audioPlayer.play()
    }
  }
}

上一首/下一首功能

添加切换上一首和下一首的方法:

vue实现歌曲切换

methods: {
  prevSong() {
    let newIndex = this.currentSongIndex - 1
    if (newIndex < 0) newIndex = this.songs.length - 1
    this.playSong(newIndex)
  },

  nextSong() {
    let newIndex = this.currentSongIndex + 1
    if (newIndex >= this.songs.length) newIndex = 0
    this.playSong(newIndex)
  }
}

自动播放下一首

监听音频结束事件,实现自动播放下一首:

mounted() {
  this.audioPlayer.addEventListener('ended', this.nextSong)
}

模板部分

在模板中添加播放控制按钮和歌曲信息显示:

vue实现歌曲切换

<template>
  <div class="player">
    <div class="song-info">
      <h3>{{ songs[currentSongIndex].title }}</h3>
      <p>{{ songs[currentSongIndex].artist }}</p>
    </div>

    <div class="controls">
      <button @click="prevSong">上一首</button>
      <button @click="audioPlayer.paused ? audioPlayer.play() : audioPlayer.pause()">
        {{ audioPlayer.paused ? '播放' : '暂停' }}
      </button>
      <button @click="nextSong">下一首</button>
    </div>

    <div class="playlist">
      <ul>
        <li v-for="(song, index) in songs" 
            :key="song.id" 
            @click="playSong(index)"
            :class="{ active: currentSongIndex === index }">
          {{ song.title }} - {{ song.artist }}
        </li>
      </ul>
    </div>
  </div>
</template>

样式优化

添加基本样式增强用户体验:

.player {
  max-width: 400px;
  margin: 0 auto;
}

.song-info {
  text-align: center;
  margin-bottom: 20px;
}

.controls {
  display: flex;
  justify-content: center;
  gap: 10px;
  margin-bottom: 20px;
}

.playlist ul {
  list-style: none;
  padding: 0;
}

.playlist li {
  padding: 8px;
  cursor: pointer;
  border-bottom: 1px solid #eee;
}

.playlist li:hover {
  background-color: #f5f5f5;
}

.playlist li.active {
  background-color: #e0f7fa;
  font-weight: bold;
}

音量控制

可选添加音量控制功能:

methods: {
  setVolume(volume) {
    this.audioPlayer.volume = volume
  }
}
<input type="range" min="0" max="1" step="0.1" 
       v-model="volume" 
       @input="setVolume(volume)">

进度条显示

可选添加播放进度显示:

data() {
  return {
    // ...
    currentTime: 0,
    duration: 0
  }
},

mounted() {
  this.audioPlayer.addEventListener('timeupdate', () => {
    this.currentTime = this.audioPlayer.currentTime
    this.duration = this.audioPlayer.duration
  })
}
<div class="progress">
  <span>{{ formatTime(currentTime) }}</span>
  <input type="range" min="0" :max="duration" 
         v-model="currentTime"
         @change="audioPlayer.currentTime = currentTime">
  <span>{{ formatTime(duration) }}</span>
</div>

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

相关文章

vue实现token

vue实现token

Vue 中实现 Token 认证的方法 在 Vue 中实现 Token 认证通常涉及前端与后端的交互,包括 Token 的获取、存储、发送和刷新等操作。以下是具体实现步骤: 获取 Token 用户登…

vue实现RTMP

vue实现RTMP

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

vue 实现注册

vue 实现注册

Vue 实现注册功能 注册功能通常包括表单验证、数据提交和用户反馈。以下是实现注册功能的步骤: 1. 创建注册表单组件 在 Vue 项目中创建一个注册表单组件,包含用户名、邮箱、密码等输入字段。…

vue实现getapp

vue实现getapp

Vue 中实现全局获取应用实例的方法 在 Vue 3 中,可以通过 getCurrentInstance 方法获取当前组件实例,但需要明确其使用场景和限制。以下是具体实现方式: 方法一:通过 ge…

实现vue cli

实现vue cli

安装 Vue CLI 确保系统中已安装 Node.js(版本建议 12.x 或更高)。通过 npm 全局安装 Vue CLI: npm install -g @vue/cli 安装完成后验证版本:…

vue拖动实现

vue拖动实现

实现 Vue 拖动功能的方法 使用 HTML5 拖放 API 在 Vue 中可以利用 HTML5 的原生拖放 API 实现拖动功能。通过 draggable 属性标记可拖动元素,结合 @dragsta…