vue + element ui實(shí)現(xiàn)播放器功能的實(shí)例代碼
沒(méi)有效果圖的展示都是空口無(wú)憑

1.基于audio并結(jié)合elementUI 的進(jìn)度條實(shí)現(xiàn)
2.實(shí)現(xiàn)了播放器基本的功能,播放/暫停、快進(jìn)、靜音、調(diào)節(jié)聲音大小、下載等功能
html代碼,關(guān)鍵部分已加注釋
<div class="right di main-wrap" v-loading="audio.waiting">
<!-- 此處的ref屬性,可以很方便的在vue組件中通過(guò) this.$refs.audio獲取該dom元素 -->
<audio ref="audio" class="dn"
:src="url" :preload="audio.preload"
@play="onPlay"
@error="onError"
@waiting="onWaiting"
@pause="onPause"
@timeupdate="onTimeupdate"
@loadedmetadata="onLoadedmetadata"
></audio>
<div class="w-full">
<div class="flex items-center w-10/12 mx-auto">
<!-- 當(dāng)前時(shí)間 -->
<el-tag type="info">{{ audio.currentTime | formatSecond}}</el-tag>
<!-- 滾動(dòng)條 -->
<el-slider v-show="!controlList.noProcess" v-model="sliderTime" :format-tooltip="formatProcessToolTip" @change="changeCurrentTime" class="slider_time"></el-slider>
<!-- 總時(shí)長(zhǎng) -->
<el-tag type="info">{{ audio.maxTime | formatSecond }}</el-tag>
</div>
<div class="mt-3 flex items-center w-1/2 mx-auto justify-around">
<!-- 播放/暫停 -->
<el-button type="text" @click="startPlayOrPause">{{audio.playing | transPlayPause}}</el-button>
<!-- 快進(jìn) -->
<el-button v-show="!controlList.noSpeed" type="text" @click="changeSpeed">{{audio.speed | transSpeed}}</el-button>
<!-- 靜音 -->
<el-button v-show="!controlList.noMuted" type="text" @click="startMutedOrNot">{{audio.muted | transMutedOrNot}}</el-button>
<!-- 音量 -->
<div class="flex items-center">
<span class="mr-2 text-sm">音量</span>
<el-slider v-show="!controlList.noVolume" v-model="volume" :format-tooltip="formatVolumeToolTip" @change="changeVolume" class="slider_voice"></el-slider>
</div>
<!-- 下載 -->
<a :href="url" rel="external nofollow" v-show="!controlList.noDownload" target="_blank" class="download text-sm" download>下載</a>
</div>
</div>
</div>
js代碼
<script>
// 將整數(shù)轉(zhuǎn)換成 時(shí):分:秒的格式
function realFormatSecond(second) {
var secondType = typeof second
if (secondType === 'number' || secondType === 'string') {
second = parseInt(second)
var hours = Math.floor(second / 3600)
second = second - hours * 3600
var mimute = Math.floor(second / 60)
second = second - mimute * 60
return hours + ':' + ('0' + mimute).slice(-2) + ':' + ('0' + second).slice(-2)
} else {
return '0:00:00'
}
}
export default {
name: 'voicetotext',
props: {
theSpeeds: {
type: Array,
default () {
return [1, 1.5, 2]
}
},
theControlList: {
type: String,
default: ''
}
},
data(){
return{
url: 'https://wdd.js.org/element-audio/static/falling-star.mp3',
audio: {
currentTime: 0,
maxTime: 0,
playing: false,
muted: false,
speed: 1,
waiting: true,
preload: 'auto'
},
sliderTime: 0,
volume: 100,
speeds: this.theSpeeds,
controlList: {
// 不顯示下載
noDownload: false,
// 不顯示靜音
noMuted: false,
// 不顯示音量條
noVolume: false,
// 不顯示進(jìn)度條
noProcess: false,
// 只能播放一個(gè)
onlyOnePlaying: false,
// 不要快進(jìn)按鈕
noSpeed: false
}
}
},
methods:{
setControlList () {
let controlList = this.theControlList.split(' ')
controlList.forEach((item) => {
if(this.controlList[item] !== undefined){
this.controlList[item] = true
}
})
},
changeSpeed() {
let index = this.speeds.indexOf(this.audio.speed) + 1
this.audio.speed = this.speeds[index % this.speeds.length]
this.$refs.audio.playbackRate = this.audio.speed
},
startMutedOrNot() {
this.$refs.audio.muted = !this.$refs.audio.muted
this.audio.muted = this.$refs.audio.muted
},
// 音量條toolTip
formatVolumeToolTip(index) {
return '音量條: ' + index
},
// 進(jìn)度條toolTip
formatProcessToolTip(index = 0) {
index = parseInt(this.audio.maxTime / 100 * index)
return '進(jìn)度條: ' + realFormatSecond(index)
},
// 音量改變
changeVolume(index = 0) {
this.$refs.audio.volume = index / 100
this.volume = index
},
// 播放跳轉(zhuǎn)
changeCurrentTime(index) {
this.pausePlay()
this.$refs.audio.currentTime = parseInt(index / 100 * this.audio.maxTime)
},
startPlayOrPause() {
return this.audio.playing ? this.pausePlay() : this.startPlay()
},
// 開(kāi)始播放
startPlay() {
this.$refs.audio.play()
},
// 暫停
pausePlay() {
this.$refs.audio.pause()
},
// 當(dāng)音頻暫停
onPause () {
this.audio.playing = false
},
// 當(dāng)發(fā)生錯(cuò)誤, 就出現(xiàn)loading狀態(tài)
onError () {
this.audio.waiting = true
},
// 當(dāng)音頻開(kāi)始等待
onWaiting (res) {
console.log(res)
},
// 當(dāng)音頻開(kāi)始播放
onPlay (res) {
console.log(res)
this.audio.playing = true
this.audio.loading = false
if(!this.controlList.onlyOnePlaying){
return
}
let target = res.target
let audios = document.getElementsByTagName('audio');
[...audios].forEach((item) => {
if(item !== target){
item.pause()
}
})
},
// 當(dāng)timeupdate事件大概每秒一次,用來(lái)更新音頻流的當(dāng)前播放時(shí)間
onTimeupdate(res) {
// console.log('timeupdate')
// console.log(res)
this.audio.currentTime = res.target.currentTime
this.sliderTime = parseInt(this.audio.currentTime / this.audio.maxTime * 100)
},
// 當(dāng)加載語(yǔ)音流元數(shù)據(jù)完成后,會(huì)觸發(fā)該事件的回調(diào)函數(shù)
// 語(yǔ)音元數(shù)據(jù)主要是語(yǔ)音的長(zhǎng)度之類的數(shù)據(jù)
onLoadedmetadata(res) {
this.audio.waiting = false
this.audio.maxTime = parseInt(res.target.duration)
}
},
filters: {
formatSecond(second = 0) {
return realFormatSecond(second)
},
transPlayPause(value) {
return value ? '暫停' : '播放'
},
transMutedOrNot(value) {
return value ? '放音' : '靜音'
},
transSpeed(value) {
return '快進(jìn): x' + value
}
},
created() {
this.setControlList()
}
}
</script>
CSS代碼用的是SCSS
<style scoped lang="scss">
.right{
width: 100%;
padding: 10px 15px;
display: inline-block;
.slider {
display: inline-block;
position: relative;
top: 14px;
margin-left: 15px;
}
.slider_time{
width: 550px;
margin: 0 10px;
}
.slider_voice{
width: 80px;
}
.download {
color: #409EFF;
margin-left: 15px;
}
.dn{
display: none;
}
}
</style>
另附一首優(yōu)美測(cè)試音樂(lè)
https://wdd.js.org/element-audio/static/falling-star.mp3
到此這篇關(guān)于vue + element ui實(shí)現(xiàn)播放器功能的文章就介紹到這了,更多相關(guān)vue element ui播放器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue中如何實(shí)現(xiàn)pdf文件預(yù)覽的方法
這篇文章主要介紹了vue中如何實(shí)現(xiàn)pdf文件預(yù)覽的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
VUE 實(shí)現(xiàn)復(fù)制內(nèi)容到剪貼板的兩種方法
這篇文章主要介紹了VUE 實(shí)現(xiàn)復(fù)制內(nèi)容到剪貼板功能,本文通過(guò)兩種方法,給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-04-04
Vue中如何動(dòng)態(tài)顯示表格內(nèi)容
這篇文章主要介紹了Vue中如何動(dòng)態(tài)顯示表格內(nèi)容問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
詳解Vue2?watch監(jiān)聽(tīng)props的值
再次遇到監(jiān)聽(tīng)子組件收到父組件傳過(guò)來(lái)的值,如果這個(gè)值變化,頁(yè)面中的值發(fā)現(xiàn)是不會(huì)跟著同步變化的,本文給大家介紹Vue2?watch監(jiān)聽(tīng)props的值,感興趣的朋友一起看看吧2023-12-12
vue線上部署請(qǐng)求接口報(bào)錯(cuò)net::ERR_CONNECTION_REFUSED
vue線上部署請(qǐng)求接口報(bào)錯(cuò)net::ERR_CONNECTION_REFUSED問(wèn)題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
vue項(xiàng)目中如何將當(dāng)前頁(yè)面生成圖片
這篇文章主要介紹了vue項(xiàng)目中如何將當(dāng)前頁(yè)面生成圖片問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
vue?element-plus圖片預(yù)覽實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于vue?element-plus圖片預(yù)覽實(shí)現(xiàn)的相關(guān)資料,最近的項(xiàng)目中有圖片預(yù)覽的場(chǎng)景,也是踩了一些坑,在這里總結(jié)一下,需要的朋友可以參考下2023-07-07
一步步教你利用webpack如何搭一個(gè)vue腳手架(超詳細(xì)講解和注釋)
這篇文章主要給大家介紹了軟玉利用webpack如何搭一個(gè)vue腳手架的相關(guān)資料,文中有超詳細(xì)講解和注釋,對(duì)大家學(xué)習(xí)或者使用webpack具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-01-01
vue-cli對(duì)element-ui組件進(jìn)行二次封裝的實(shí)戰(zhàn)記錄
組件類似于需要多個(gè)地方用到的方法,在Vue中組件就是一種復(fù)用(經(jīng)常使用)一個(gè)功能的手段,下面這篇文章主要給大家介紹了關(guān)于Vue?element?ui二次封裝的相關(guān)資料,需要的朋友可以參考下2022-06-06

