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

效果图如下:
在这里插入图片描述
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>
  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值