您好,登錄后才能下訂單哦!
最近在寫一個基于 github-page 和 gist 的博客, 想加個音樂播放器, 做了一個早上, 發出來分享一下
演示地址 https://github-laziji.github.io
效果
使用到的組件
element組件
html5組件
audio
實現代碼
更詳細的實現可以看 https://github.com/GitHub-Laziji/vblog
<template> <div> <el-row> <el-col :span="4"> <el-popover placement="top-start" trigger="hover"> <div > <el-progress color="#67C23A" type="circle" :percentage="music.volume"></el-progress><br> <el-button @click="changeVolume(-10)" icon="el-icon-minus" circle></el-button> <el-button @click="changeVolume(10)" icon="el-icon-plus" circle></el-button> </div> <el-button @click="play" id="play" slot="reference" :icon="music.isPlay?'el-icon-refresh':'el-icon-caret-right'" circle></el-button> </el-popover> </el-col> <el-col :span="14" > <el-slider @change="changeTime" :format-tooltip="formatTime" :max="music.maxTime" v-model="music.currentTime" ></el-slider> </el-col> <el-col :span="6" > {{formatTime(music.currentTime)}}/{{formatTime(music.maxTime)}} </el-col> </el-row> <audio ref="music" loop autoplay> <source src="http://sc1.111ttt.cn:8282/2018/1/03m/13/396131232171.m4a?tflag=1519095601&pin=6cd414115fdb9a950d827487b16b5f97#.mp3" type="audio/mpeg"> </audio> </div> </template> <script> export default{ data(){ return { music:{ isPlay:false, currentTime:0, maxTime:0, volume:100 } } }, mounted(){ this.$nextTick(()=>{ setInterval(this.listenMusic,1000) }) }, methods:{ listenMusic(){ if(!this.$refs.music){ return } if(this.$refs.music.readyState){ this.music.maxTime = this.$refs.music.duration } this.music.isPlay=!this.$refs.music.paused this.music.currentTime = this.$refs.music.currentTime }, play(){ if(this.$refs.music.paused){ this.$refs.music.play() }else{ this.$refs.music.pause() } this.music.isPlay=!this.$refs.music.paused this.$nextTick(()=>{ document.getElementById('play').blur() }) }, changeTime(time){ this.$refs.music.currentTime = time }, changeVolume(v){ this.music.volume += v if(this.music.volume>100){ this.music.volume = 100 } if(this.music.volume<0){ this.music.volume = 0 } this.$refs.music.volume = this.music.volume/100 }, formatTime(time){ let it = parseInt(time) let m = parseInt(it/60) let s = parseInt(it%60) return (m<10?"0":"")+parseInt(it/60)+":"+(s<10?"0":"")+parseInt(it%60) } } } </script>
總結
以上所述是小編給大家介紹的基于vue-element組件實現音樂播放器功能,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復大家的!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。