亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

VUE中使用Wavesurfer.js實(shí)現(xiàn)可視化音頻波形功能

 更新時(shí)間:2025年06月04日 09:13:35   作者:在飄著呢6751  
這篇文章主要介紹了VUE中使用Wavesurfer.js實(shí)現(xiàn)可視化音頻波形功能的相關(guān)資料,通過npm安裝、創(chuàng)建組件、傳入音頻路徑參數(shù),并配置波紋顏色、大小等樣式屬性,需要的朋友可以參考下

最近有一個(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)文章

最新評論