vue 音乐播放器上一首 下一首切换

vue 音乐播放器上一首 下一首切换

根据自定义属性的值找到元素

我是使用监听来实现切换的,将v-for循环列表的index存储在vuex中,点击上一首或下一首改变index的值,在另一个组件中监听index,获取当前index值所在的元素,再获取元素上的歌曲的id,播放歌曲。

考虑到还有歌单中的歌曲和搜索的歌曲,感觉这么做要好一些

代码只截取了片段

歌曲组件

//自定义属性将id和index绑定
<li
  v-for="(item, index) in ReMusics"
  :key="index"
  :data-musicid="item.id"
  :data-index="index"
  ref="remusic"
></li>
  computed: {
   
    lsUpDown() {
   
      return this.$store.state.reMusicIndex;
    },
  },
  watch: {
   
    lsUpDown
  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,这里提供一个基于Vue.js的简单音乐播放器示例。 首先,我们需要准备一个音乐文件和一些歌曲信息。在这个示例中,我们将使用以下歌曲信息: ```javascript [ { id: 1, title: "Shape of You", artist: "Ed Sheeran", album: "÷", duration: 233 url: "https://xxx.xxx/shape_of_you.mp3" }, { id: 2, title: "Sugar", artist: "Maroon 5", album: "V", duration: 235 url: "https://xxx.xxx/sugar.mp3" }, // ... ] ``` 接下来,我们可以编写一个音乐播放器组件`MusicPlayer`,如下所示: ```html <template> <div class="music-player"> <div class="music-info"> <div class="music-title">{{ currentSong.title }}</div> <div class="music-artist">{{ currentSong.artist }}</div> </div> <div class="music-controls"> <button class="play-btn" :class="{ 'playing': isPlaying }" @click="togglePlay"></button> <div class="progress-bar"> <div class="progress" :style="{ width: progress + '%' }"></div> </div> <div class="time">{{ currentTime }} / {{ duration }}</div> </div> </div> </template> <script> export default { data() { return { songs: [ { id: 1, title: "Shape of You", artist: "Ed Sheeran", album: "÷", duration: 233, url: "https://xxx.xxx/shape_of_you.mp3" }, { id: 2, title: "Sugar", artist: "Maroon 5", album: "V", duration: 235, url: "https://xxx.xxx/sugar.mp3" }, // ... ], currentSongIndex: 0, isPlaying: false, currentTime: 0, duration: 0, progress: 0, audio: null }; }, computed: { currentSong() { return this.songs[this.currentSongIndex]; } }, mounted() { this.audio = new Audio(); this.audio.addEventListener("timeupdate", this.updateTime); this.audio.addEventListener("ended", this.nextSong); this.loadSong(); }, methods: { loadSong() { this.audio.src = this.currentSong.url; this.audio.load(); this.duration = this.currentSong.duration; }, togglePlay() { if (this.isPlaying) { this.audio.pause(); } else { this.audio.play(); } this.isPlaying = !this.isPlaying; }, updateTime() { this.currentTime = Math.floor(this.audio.currentTime); this.progress = (this.currentTime / this.duration) * 100; }, nextSong() { this.currentSongIndex = (this.currentSongIndex + 1) % this.songs.length; this.loadSong(); this.audio.play(); this.isPlaying = true; } } }; </script> <style scoped> .music-player { display: flex; align-items: center; justify-content: center; flex-direction: column; width: 300px; padding: 20px; background-color: #f2f2f2; } .music-info { text-align: center; margin-bottom: 20px; } .music-title { font-size: 24px; font-weight: bold; } .music-artist { font-size: 16px; color: #999; } .music-controls { display: flex; align-items: center; justify-content: center; flex-direction: column; } .play-btn { display: block; width: 80px; height: 80px; border: none; border-radius: 50%; background-color: #f44336; background-image: url("https://cdn3.iconfinder.com/data/icons/google-material-design-icons/48/ic_play_circle_filled_48px-512.png"); background-size: 60%; background-repeat: no-repeat; background-position: center; cursor: pointer; transition: background-color 0.2s ease-in-out; } .playing { background-color: #4caf50; } .playing:before { content: ""; display: block; width: 0; height: 0; border-top: 25px solid transparent; border-left: 40px solid #fff; border-bottom: 25px solid transparent; margin-left: 20px; } .progress-bar { width: 100%; height: 4px; background-color: #ccc; margin: 10px 0; } .progress { width: 0; height: 100%; background-color: #f44336; } .time { font-size: 14px; color: #999; } </style> ``` 在这个示例中,我们定义了一个`MusicPlayer`组件,并使用`data`属性来存储歌曲列表、当前歌曲索引、播放状态、当前播放时间、音频对象等信息。我们还使用计算属性`currentSong`来获取当前歌曲的信息。 在`mounted`钩子函数中,我们创建了一个`Audio`对象,并添加了`timeupdate`和`ended`事件的监听器,分别用于更新当前播放时间和切换到下一首歌曲。我们还调用了`loadSong`方法来加载当前歌曲信息,并将歌曲时长设置为`duration`属性的值。 在`methods`属性中,我们定义了一些方法来控制音乐播放器的行为。`loadSong`方法用于加载当前歌曲信息,`togglePlay`方法用于切换播放状态,`updateTime`方法用于更新当前播放时间和进度条,`nextSong`方法用于切换到下一首歌曲。 最后,我们在模板中使用了一些Vue.js的指令和事件监听器,来渲染音乐播放器的界面并控制其行为。 以上就是一个简单的基于Vue.js的音乐播放器示例,您可以根据自己的需求进行修改和扩展。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值