40,'middle':duration>20&&duration<41,'small':duration<21}">_wavesurfer.js 实时获取语音波动">
当前位置:   article > 正文

vue中使用wavesurfer.js实现语音播放波形显示_wavesurfer.js 实时获取语音波动

wavesurfer.js 实时获取语音波动

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

npm install wavesurfer.js --save
  • 1

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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104

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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/130189
推荐阅读
  

闽ICP备14008679号