声纹可视化工具wavesurfer播放与暂停

声纹可视化工具wavesurfer播放与暂停

一、安装wavesurfer

npm install wavesurfer.js

二、引入与创建

<template>      
  <div id="app">      
    <input type="file" @change="onFileChange" />      
    <button @click="playAudio" class="audio-button">播放</button>      
    <button @click="pauseAudio" class="audio-button">暂停</button>      
    <div id="waveform"></div>      
  </div>      
</template>   


<style scoped>  
.audio-button {  
  background-color: #bfb0fb; /* 这将设置按钮的背景色为音量图的颜色 */  
}  
</style>  


<script>      
import Wavesurfer from "wavesurfer.js";      

export default {      
  data() {      
    return {      
      file: null,      
      waveform: null,      
      wavesurfer: null,      
      isPlaying: false,      
    };      
  },      
  methods: {      
    onFileChange(e) {      
      this.file = e.target.files[0];      
      this.loadWaveform();      
    },    
    loadWaveform() {  
      if (this.wavesurfer) {    
        this.wavesurfer.destroy();    
      }    
      const reader = new FileReader();    
      reader.onload = (e) => {    
        this.wavesurfer = Wavesurfer.create({    
          container: document.getElementById("waveform"),    
          waveHeight: 100,    
          progressColor: "#d384fe", // 这将设置进度条的颜色为播放按钮的颜色  
          cursorColor: "#d0bdda",    
          hideScrollbar: true,    
          responsive: true,    
          waveColor: "#bfb0fb", // 这将设置音量图的颜色为按钮的颜色  
        });    
        this.wavesurfer.load(e.target.result);    
      };    
      reader.readAsDataURL(this.file);    
    },      
    playAudio() {      
      if (!this.isPlaying) {      
        this.wavesurfer.play();      
        this.isPlaying = true;      
      }      
    },      
    pauseAudio() {      
      if (this.isPlaying) {      
        this.wavesurfer.pause();      
        this.isPlaying = false;      
      }      
    },      
  },      
};      
</script> 


三、结果展示
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值