vue中使用wavesurfer.js实现语音播放波形显示

本文介绍了如何在Vue项目中使用wavesurfer.js插件来实现实时音频播放控制,包括安装步骤、组件结构与样式设置,以及在实际场景中的调用方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果图如下:
在这里插入图片描述
1、第一步:使用以下命令安装wavesurfer.js插件库

npm install wavesurfer.js --save

2、第二步:在compenets文件夹创建voice.vue

<template>
  <div :class="{'voice_wrap':true,'large':duration>40,'middle':duration>20&&duration<41,'small':duration<21}">
    <img v-if="!isplay" src="@/assets/img/play.png" alt="" @click="play">
    <img v-else src="@/assets/img/pause.png" alt="" @click="pause">
    <div class="voice_time_line" :id="'wave'+id"></div>
    <div class="duration">{{duration}}"</div>
  </div>
</template>

<script>
//引入wavesurfer.js
import WaveSurfer from 'wavesurfer.js' //导入wavesurfer.js
export default {
  props:{
    id:Number,
    url:String,
    duration:Number
  },
  data(){
    return {
      isplay:false,
      wavesurfer: null,
    }
  },
  mounted(){
    let that = this;
    this.$nextTick(()=>{
      that.wavesurfer = WaveSurfer.create({
        height:24,
		container: '#wave' + that.id,
        cursorColor:'transparent',
        maxCanvasWidth:60,
        progressColor:'rgba(255,255,255,1)',
        waveColor:'rgba(255,255,255,0.4)',
        barGap:1.8,
        barWidth:1.2,
        barHeight:16,
        barMinHeight:0.5
      });
      // 特别提醒:如果是本地文件此处需要使用require(相对路径),否则会报错
      that.wavesurfer.load(that.url);
      that.wavesurfer.on('finish', function () {
		  console.log('播放完毕:finish');
        that.isplay = false;
        that.wavesurfer.stop();
      });
    })
  },
  methods:{
    play(){
      this.isplay = true;
      this.wavesurfer.play();
    },
    pause(){
      this.isplay = false;
      this.wavesurfer.pause();
    }
  }
}
</script>

<style lang="scss">
  .voice_wrap{
    background: #94BDEC;
    border-radius: 18px;
    height: 36px;
    display: flex;
    align-items: center;
    padding: 0 12px;
    box-sizing: border-box;
    img{
      width: 24px;
      height: 24px;
      margin-right: 4px;
    }
    .voice_time_line{
      height: 24px;
    }
    .duration{
      font-size: 16px;
      line-height: 22px;
      color: #fff;
      margin-left: 8px;
    }
  }
  .voice_wrap.large{
    width: 262px;
    .voice_time_line{
      width: 177px;
    }
  }
  .voice_wrap.middle{
    width: 203px;
    .voice_time_line{
      width: 118px;
    }
  }
  .voice_wrap.small{
    width: 144px;
    .voice_time_line{
      width: 59px;
    }
  }
</style>

3、第三步:在要使用该组件的地方调用

<template>
  <div>
   <Voice :ref="'voice'+id" :id="id" :url="audio.url" :duration="audio.du"></Voice>
  </div>
</template>

<script>
import Voice from '@/components/commentsVoice/index.vue'
export default {
  data(){
    return{
      id:12,
      audio:{
      	url:'',
      	du:''
	  }
    }
  },
  mounted(){
  
  },
  methods:{
   
  },
  components:{
    Voice
  }
}
</script>
<style lang="scss" scoped>
  
</style>
### Wavesurfer.js 音频流处理与可视化 Wavesurfer.js 是一款用于创建交互式音频波形可视化的 JavaScript 库[^1]。该库支持多种功能,包括但不限于实时音频分析、回放控制以及自定义插件开发。 #### 安装 Wavesurfer.js 为了使用 Wavesurfer.js,在 HTML 文件头部引入必要的 CSS 和 JS 资源文件: ```html <link rel="stylesheet" href="https://unpkg.com/wavesurfer.js/dist/wavesurfer.min.css"> <script src="https://unpkg.com/wavesurfer.js"></script> ``` 对于现代前端框架集成(如 Vue),可以考虑通过 npm 或 yarn 来安装依赖包[^3]: ```bash npm install wavesurfer.js --save # or yarn add wavesurfer.js ``` #### 初始化并配置 Wavesurfer 实例 下面是一个简单的例子来展示如何初始化一个 Wavesurfer 对象,并加载远程 MP3 文件作为数据源: ```javascript // 创建一个新的 WaveSurfer 实例并与 DOM 中指定 ID 的容器关联起来 var wavesurfer = WaveSurfer.create({ container: '#waveform', waveColor: 'violet', progressColor: 'purple' }); // 加载音频文件 wavesurfer.load('http://example.com/audio.mp3'); ``` 这段代码会生成一段代表声音强度变化的图形化表示形式——即所谓的“波形”。用户可以通过点击拖拽等方式直接操作这个图像来进行播放位置的选择等互动行为。 #### 处理音频流 除了静态音频文件外,Wavesurfer.js 还能够连接到 Web Audio API 提供的数据流接口,从而实现更复杂的应用场景,比如直播广播中的音量监测或是语音识别系统的输入预览等功能。要启用此特性,只需设置 `mediaStream` 参数即可[^2]: ```javascript navigator.mediaDevices.getUserMedia({ audio: true }) .then(function(stream){ var wavesurfer = WaveSurfer.create({ container: '#waveform', mediaStream: stream, waveColor: 'red', progressColor: 'pink' }); }); ``` 上述脚本片段展示了怎样利用浏览器内置的方法获取用户的麦克风权限并将返回的结果传递给 Wavesurfer 以构建基于媒体流的工作流程。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值