淘先锋技术网

首页 1 2 3 4 5 6 7

vue+elementUI使用Wavesurfer.js音频可视化

效果图
在这里插入图片描述
安装 wavesurfer.js

 npm install wavesurfer.js --save

(datav安装)

 npm install @jiaminghi/data-view

在使用界面引用

import WaveSurfer from 'wavesurfer.js' //导入wavesurfer.js
import Timeline from 'wavesurfer.js/dist/plugin/wavesurfer.timeline.js' //导入时间轴插件

在使用界面引用

// 将自动注册所有组件为全局组件
import dataV from '@jiaminghi/data-view'
Vue.use(dataV)

HTML

<template>
  <div id="aiccAudio">
    <el-card>
      <div id="wavefrom" ref="wavefrom" @click="getTime">
      </div>
    </el-card>
    <div class="audio-controlBar">
      <!-- 播放/暂停按钮 -->
      <button @click="playMusic">
        <i v-if="videoPlay" class="el-icon-video-play"></i>
        <i v-else class="el-icon-video-pause"></i>
      </button>
      <!-- 时间  -->
      <div class="audio-time">
        <div class="audio-current-time" aria-label="time">{{currentTime}}</div>
        <span class="audio-fen-line">|</span>
        <div class="audio-duration" aria-label="duration">{{duration}}</div>
      </div>
      <!-- 倍速  -->
      <el-select class="audio-speed" v-model="speedValue" @change="speedChange" style="width: 60px;">
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
      <span class="audio-line">|</span>
      <!--  音量  -->
      <div class="audio-volume">
        <img v-if="audioVolumeStatus == true" @click="volumeStatusChange('open')" class="audio-volume-img" src="../../assets/aiccAssets/softphoneWorkbench/volumeOpen.png">
        <img v-else class="audio-volume-img" @click="volumeStatusChange('close')" src="../../assets/aiccAssets/softphoneWorkbench/volumeClose.png">
        <el-slider class="audio-volume-slider" v-model="volume" @input="volumeChange"></el-slider>
      </div>
    </div>
  </div>
</template>

JS

export default {
  name: 'AiccAudio',
  props: {
    src: {
      type: String
    },
    recordPath: {
      type: String
    }
  },
  data() {
    return {
      wavesurfer: null,
      timer: null,
      options: [
        {
          value: '0.5',
          label: '0.5',
        }, {
          value: '1.0',
          label: '1.0',
        }, {
          value: '1.5',
          label: '1.5',
        }, {
          value: '2.0',
          label: '2.0',
        }
      ],
      speedValue: '1.0',
      currentTime: '00:00:00', //当前播放
      duration: '00:00:00', //总时长
      volume: 30,//音量,
      audioVolumeStatus: true, //是否静音
      videoPlay: true,
    }
  },
  created() {
    this.keyDown();
  },
  mounted() {
    this.$nextTick(()=> {
      if(this.recordPath != null && this.recordPath != '') {
        this.wavesurfer = WaveSurfer.create({
          container: this.$refs.wavefrom,
          height: 35,
          barHeight: 10, // 波形条的高度
          barWidth: 1, //宽度
          barGap: 2,
          waveColor: '#409EFF',
          progressColor: '#1ae1fe',
          cursorColor: '#3be1c4', //指示播放头位置的光标填充颜色
          backend: 'MediaElement',
          mediaControls: false,
          audioRate: this.speedValue,
          hideScrollbar: true,
          setPlaybackRate: [0.5,1.0,1.5,2.0],
          //使用时间轴插件
          plugins: [
            // TimeLine.create({
            //   container: '#wave-timeline',
            //   secondaryColor: '#fff',//次要时间标签颜色
            //   secondaryFontColor: '#fff',
            //   primaryColor: '#fff',//主要时间标签颜色
            //   primaryFontColor: '#fff',
            //   // labelPadding: 2
            // }),
            // Spectrogram.create({
            //   container: "#wave-spectrogram",
            //   labels: true,
            //   fftSamples: 512,
            //   colorMap: this.colorMap
            // })
          ],
        })
        this.wavesurfer.load(this.src)
        //音频加载完成触发
        this.wavesurfer.on("ready", () => {
          this.wavesurfer.setVolume(this.volume / 100);
          this.duration = formatSecond(this.wavesurfer.getDuration())
        });
      }
    })
  },
  methods: {
    playMusic() {
      if(this.recordPath != null && this.recordPath != '') {
        //"播放/暂停"按钮的单击触发事件,暂停的话单击则播放,正在播放的话单击则暂停播放
        this.wavesurfer.playPause.bind(this.wavesurfer)();
        //每秒获取进度 有时间轴时使用
        // this.getProcess();
        //判断是否播放
        this.videoPlay = this.wavesurfer.isPlaying() == true ? false : true;
        //音频播放时触发
        this.wavesurfer.on("audioprocess", () => {
          this.currentTime = formatSecond(this.wavesurfer.getCurrentTime())
        })
        //结束播放
        this.wavesurfer.on("finish", () => {
          this.wavesurfer.stop();
          this.videoPlay = true;
          this.currentTime = '00:00:00'
        });
      }
    },
    //有时间轴时使用
    // getProcess(){
    //   if(this.wavesurfer.isPlaying()){
    //     this.timer=setInterval(()=>{
    //       this.percent=(this.wavesurfer.getCurrentTime().toFixed(0)/this.wavesurfer.getDuration()*100).toFixed(0)
    //       this.config={
    //         value:this.percent
    //       }
    //     },1000)
    //   }else{
    //     clearInterval(this.timer)
    //   }
    // },
    //点击获取点击进度
    getTime(){
      if(this.recordPath != null && this.recordPath != '') {
        //加定时器,不然获取的是点击前的播放时间
        setTimeout(() => {
          this.currentTime = formatSecond(this.wavesurfer.getCurrentTime())
          ///有时间轴时使用
          // this.percent=(this.wavesurfer.getCurrentTime()/this.wavesurfer.getDuration()*100).toFixed(0)
          // this.config={
          //   value:this.percent
          // }
        }, 100)
      }
    },
    //按键跳转进度
    // jump(e){
    // this.wavesurfer.play([e.target.innerHTML-0])
    // this.percent=(this.wavesurfer.getCurrentTime().toFixed(0)/this.wavesurfer.getDuration()*100).toFixed(0)
    // this.config={
    //   value:this.percent
    // }
    // this.getProcess()
    // },
    // 监听键盘
    keyDown() {
      if(this.recordPath != null && this.recordPath != '') {
        document.onkeydown = (e) => {
          //事件对象兼容
          var eCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
          //键盘按键判断:左箭头-37;上箭头-38;右箭头-39;下箭头-40
          //左
          if (eCode == 37) {
            // 按下左箭头
            this.wavesurfer.skip(-6)
            this.getTime()
          } else if (eCode == 39) {
            // 按下右箭头
            this.wavesurfer.skip(6)
            this.getTime()
          }
        }
      }
    },
    //倍速
    speedChange(val) {
      if(this.recordPath != null && this.recordPath != '') {
        this.speedValue = val;
        this.wavesurfer.setPlaybackRate(this.speedValue)
      }
    },
    //音量大小改变
    volumeChange(val) {
      if(this.recordPath != null && this.recordPath != '') {
        this.volume = val;
        if (this.wavesurfer != null) {
          this.wavesurfer.setVolume(val / 100);
        }
        if (val == '0') {
          this.audioVolumeStatus = false
        } else {
          this.audioVolumeStatus = true
        }
      }
    },
    //静音开启关闭
    volumeStatusChange(status) {
      if(this.recordPath != null && this.recordPath != '') {
        if (status == 'open') {
          this.audioVolumeStatus = false
          this.volume = 0;
          this.wavesurfer.setVolume(0);
        } else {
          this.audioVolumeStatus = true
          this.volume = 30;
          this.wavesurfer.setVolume(30 / 100);
        }
      }
    }
  }
}

css

<style lang="scss" scoped>
/deep/ .audio-controlBar {
  background-color: #f4f6f9;
  padding: 0 10px;
  border-radius: 10px;
  button {
    border: none;
    background-color: #f4f6f9;
  }
  i {
    //color: #95979f;
    color: #1053ee;
  }
}
//时间
.audio-time {
  display: inline-block;
  font-size: 12px;
  color: #95979f;
  margin: 0 10px;
}
.audio-current-time {
  display: inline-block;
  color: #1053ee;
}
.audio-fen-line {
  margin: 0 5px;
}
.audio-duration {
  display: inline-block;
}
//倍速
/deep/ .audio-speed {
  width: 60px;
  margin-left: 85px;
  .el-input__inner {
    background-color: #f4f6f9;
  }
}
.audio-line {
  color: #95979f;
  margin-right: 5px;
}
//音量条
/deep/ .audio-volume {
  width: 136px;
  float: right;
  .audio-volume-slider {
    width: 110px;
    float: right;
  }
}
.audio-volume-img {
  width: 15px;
  margin-top: 12px;
}
</style>

Wavesurfer.js音频播放器插件的使用教程
https://www.cnblogs.com/webhmy/p/9883150.html