VUE中使用Wavesurfer.js實(shí)現(xiàn)可視化音頻波形功能
最近有一個(gè)在線音波方法的功能需求,考慮使用Wavesurfer.js來實(shí)現(xiàn)這塊
1.首先安裝Wavesurfer.js
npm install wavesurfer.js
2.創(chuàng)建 Wavesurfer 組件
在 Vue 2項(xiàng)目中創(chuàng)建一個(gè)音頻播放器組件(如 WaveSurferPlayer.vue
),。
<template> <div style="padding: 30px"> <div ref="waveform_Ref" style="cursor: pointer"></div> <!-- 時(shí)間信息 --> <div class="time-info"> <span >當(dāng)前時(shí)間: <span style="color: #72df90">{{ formatTime(currentTime) }}</span> </span> <span>總時(shí)長: {{ formatTime(duration) }}</span> </div> <div style="padding: 30px; width: 120px; margin: auto"> <el-button type="success" icon="el-icon-video-play" @click="playMusic" circle v-if="!playing" > </el-button ><el-button v-if="playing" type="danger" icon="el-icon-video-pause" circle @click="playMusic" > </el-button> </div> </div> </template> <script> import WaveSurfer from "wavesurfer.js"; export default { name: "WaveSurferPlayer", props: { audioSrc: { type: String, required: true, }, }, data() { return { wavesurfer: null, playing: false, duration: 0, currentTime: 0, // 當(dāng)前播放時(shí)間 }; }, mounted() { this.$nextTick(() => { this.wavesurfer = WaveSurfer.create({ // 波形圖的容器 container: this.$refs.waveform_Ref, // 已播放波形的顏色 progressColor: "#13ce66", // 未播放波形的顏色 // waveColor: "lightgrey", // 波形圖的高度,單位為px // height: 10, // 是否顯示滾動條,默認(rèn)為false scrollParent: true, // 波形的振幅(高度),默認(rèn)為1 // barHeight: 0.8, // 波形條的圓角 // barRadius: 2, // 波形條的寬度 // barWidth: 1, // 波形條間的間距 // barGap: 3 // 播放進(jìn)度光標(biāo)條的顏色 cursorColor: "red", // 播放進(jìn)度光標(biāo)條的寬度,默認(rèn)為1 // cursorWidth: 10, // 播放進(jìn)度顏色 // progressColor: "blue", // 波形容器的背景顏色 // backgroundColor: "yellow", // 音頻的播放速度 // audioRate: "1", // (與區(qū)域插件一起使用)啟用所選區(qū)域的循環(huán) // loopSelection:false }); this.wavesurfer.on("error", (error) => { console.error("音頻加載失敗:", error); this.$message({ type: "error", message: "音頻加載失敗,請檢查文件路徑或網(wǎng)絡(luò)連接", }); }); this.wavesurfer.load(this.audioSrc); // 監(jiān)聽結(jié)束事件 this.wavesurfer.on("finish", () => { this.playing = false; }); // 監(jiān)聽時(shí)間更新事件 this.wavesurfer.on("audioprocess", (time) => { this.currentTime = time; }); // 監(jiān)聽暫停事件 this.wavesurfer.on("pause", () => { this.playing = false; }); // 監(jiān)聽音頻加載完成事件 this.wavesurfer.on("ready", () => { this.duration = this.wavesurfer.getDuration(); }); }); }, methods: { // 格式化時(shí)間(秒 -> 分:秒) formatTime(time) { const minutes = Math.floor(time / 60); const seconds = Math.floor(time % 60); return `${minutes}:${seconds.toString().padStart(2, "0")}`; }, playMusic() { this.wavesurfer.playPause.bind(this.wavesurfer)(); this.playing = !this.playing; }, }, beforeDestroy() { // 銷毀 Wavesurfer 實(shí)例 if (this.wavesurfer) { this.wavesurfer.destroy(); } }, }; </script> <style scoped> .time-info { font-size: 18px; color: #666; } </style>
3.在頁面中引入組件,audioUrl是音頻路徑,把音頻文件路徑audioUrl子組件傳參進(jìn)去
<WaveSurferPlayer :audioSrc="audioUrl" />
4.頁面中顯示樣式,其中波紋顏色,大小等等都可通過配置項(xiàng)設(shè)置
總結(jié)
到此這篇關(guān)于VUE中使用Wavesurfer.js實(shí)現(xiàn)可視化音頻波形功能的文章就介紹到這了,更多相關(guān)VUE Wavesurfer.js可視化音頻波形內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue動態(tài)綁定class的幾種常用方式小結(jié)
這篇文章主要介紹了vue動態(tài)綁定class的幾種常用方式,結(jié)合實(shí)例形式總結(jié)分析了vue.js常見的對象方法、數(shù)組方法進(jìn)行class動態(tài)綁定相關(guān)操作技巧,需要的朋友可以參考下2019-05-05Vuex子模塊調(diào)用子模塊的actions或mutations實(shí)現(xiàn)方式
這篇文章主要介紹了Vuex子模塊調(diào)用子模塊的actions或mutations實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10如何巧用Vue.extend繼承組件實(shí)現(xiàn)el-table雙擊可編輯(不使用v-if、v-else)
這篇文章主要給大家介紹了關(guān)于如何巧用Vue.extend繼承組件實(shí)現(xiàn)el-table雙擊可編輯的相關(guān)資料,不使用v-if、v-else,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06vue項(xiàng)目的html如何引進(jìn)public里面的js文件
這篇文章主要介紹了vue項(xiàng)目的html如何引進(jìn)public里面的js文件,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12基于vue-cli 打包時(shí)抽離項(xiàng)目相關(guān)配置文件詳解
下面小編就為大家分享一篇基于vue-cli 打包時(shí)抽離項(xiàng)目相關(guān)配置文件詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03關(guān)于Vue的URL轉(zhuǎn)跳與參數(shù)傳遞方式
這篇文章主要介紹了關(guān)于Vue的URL轉(zhuǎn)跳與參數(shù)傳遞方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03