<script src='https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.10/vue.min.js'></script> <script> /* design by Voicu Apostol. design: https://dribbble.com/shots/3533847-Mini-Music-Player I can't find any open music api or mp3 api so i have to download all musics as mp3 file. You can fork on github: https://github.com/muhammederdem/mini-player */ new Vue({ el: "#app", data() { return { audio: null, circleLeft: null, barWidth: null, duration: null, currentTime: null, isTimerPlaying: false, tracks: [ { name: "我愿你知道", artist: "Norm Ender", cover: "http://blog.img.duanshuilu.com/Snipaste_2021-07-28_21-51-49.png", source: "http://blog.img.duanshuilu.com/%E6%88%91%E6%84%BF%E4%BD%A0%E7%9F%A5%E9%81%93.mp3", url: "https://www.youtube.com/watch?v=z3wAjJXbYzA", favorited: false }, { name: "我愿你知道2", artist: "Norm Ender", cover: "http://blog.img.duanshuilu.com/Snipaste_2021-07-28_21-51-49.png", source: "http://blog.img.duanshuilu.com/%E6%88%91%E6%84%BF%E4%BD%A0%E7%9F%A5%E9%81%93.mp3", url: "https://www.youtube.com/watch?v=z3wAjJXbYzA", favorited: false } ], currentTrack: null, currentTrackIndex: 0, transitionName: null }; }, methods: { play() { if (this.audio.paused) { this.audio.play(); this.isTimerPlaying = true; } else { this.audio.pause(); this.isTimerPlaying = false; } }, generateTime() { let width = (100 / this.audio.duration) * this.audio.currentTime; this.barWidth = width + "%"; this.circleLeft = width + "%"; let durmin = Math.floor(this.audio.duration / 60); let dursec = Math.floor(this.audio.duration - durmin * 60); let curmin = Math.floor(this.audio.currentTime / 60); let cursec = Math.floor(this.audio.currentTime - curmin * 60); if (durmin < 10) { durmin = "0" + durmin; } if (dursec < 10) { dursec = "0" + dursec; } if (curmin < 10) { curmin = "0" + curmin; } if (cursec < 10) { cursec = "0" + cursec; } this.duration = durmin + ":" + dursec; this.currentTime = curmin + ":" + cursec; }, updateBar(x) { let progress = this.$refs.progress; let maxduration = this.audio.duration; let position = x - progress.offsetLeft; let percentage = (100 * position) / progress.offsetWidth; if (percentage > 100) { percentage = 100; } if (percentage < 0) { percentage = 0; } this.barWidth = percentage + "%"; this.circleLeft = percentage + "%"; this.audio.currentTime = (maxduration * percentage) / 100; this.audio.play(); }, clickProgress(e) { this.isTimerPlaying = true; this.audio.pause(); this.updateBar(e.pageX); }, prevTrack() { this.transitionName = "scale-in"; this.isShowCover = false; if (this.currentTrackIndex > 0) { this.currentTrackIndex--; } else { this.currentTrackIndex = this.tracks.length - 1; } this.currentTrack = this.tracks[this.currentTrackIndex]; this.resetPlayer(); }, nextTrack() { this.transitionName = "scale-out"; this.isShowCover = false; if (this.currentTrackIndex < this.tracks.length - 1) { this.currentTrackIndex++; } else { this.currentTrackIndex = 0; } this.currentTrack = this.tracks[this.currentTrackIndex]; this.resetPlayer(); }, resetPlayer() { this.barWidth = 0; this.circleLeft = 0; this.audio.currentTime = 0; this.audio.src = this.currentTrack.source; setTimeout(() => { if(this.isTimerPlaying) { this.audio.play(); } else { this.audio.pause(); } }, 300); }, favorite() { this.tracks[this.currentTrackIndex].favorited = !this.tracks[ this.currentTrackIndex ].favorited; } }, created() { let vm = this; this.currentTrack = this.tracks[0]; this.audio = new Audio(); this.audio.src = this.currentTrack.source; this.audio.ontimeupdate = function() { vm.generateTime(); }; this.audio.onloadedmetadata = function() { vm.generateTime(); }; this.audio.onended = function() { vm.nextTrack(); this.isTimerPlaying = true; }; // this is optional (for preload covers) for (let index = 0; index < this.tracks.length; index++) { const element = this.tracks[index]; let link = document.createElement('link'); link.rel = "prefetch"; link.href = element.cover; link.as = "image" document.head.appendChild(link) } } }); </script> </body> </html>
基于Vue.js的迷你音乐播放器
标签:
评论者:[[ schemeInfo.user.username ]]
评论内容:[[ schemeInfo.pbody ]]
评论时间:[[ schemeInfo.ptime ]]
发表你的评论:
提交评论
上一篇:
正确安装Node.js和配置cnpm
下一篇:
Vue鼠标悬停图片放大倾斜动画特效