<_vue 信号波形图">
当前位置:   article > 正文

vue+wavesurfer.js:画波形图_vue 信号波形图

vue 信号波形图
wavesurfer.js的信息
 // git
 https://github.com/katspaugh/wavesurfer.js
 // 官网
 https://wavesurfer-js.org/docs/events.html
  • 1
  • 2
  • 3
  • 4
安装
yarn add wavesurfer.js
  • 1
使用
  • template
<div class="voice-dialog-content">
   <div>
   <Button :type="!isPlaying ? 'primary' : 'warning'" shape="circle" :icon="!isPlaying ? 'ios-play' : 'ios-pause'" @click="playVoice"></Button>
</div>
<div id="waveform" ref="waveform"/>
<div class="voice-dialog-time">
   <p>{{ time }}</p>
</div>
</div>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • script
import WaveSurfer from 'wavesurfer.js'
import CursorPlugin from 'wavesurfer.js/dist/plugin/wavesurfer.cursor.js' //导入指针轴插件
export default{
    props: {
        rebot:{
            type: Object
        },
        person:{
            type:Object
        },
        url:{
            type: String,
            default: require("../../../assets/image/test.mp3")
        },
        toStopVoice:{
            type: Boolean,
            default: false
        },
        loadWave:{
            type: Boolean,
            default: true
        }
    },
    data(){
        return{
            wavesurfer: null, // 波形图对象
            time: '00:00',
            isPlaying: false
        }
    },
    mounted() {
        this.$nextTick(() => {
            // 是否重新加载
            if (this.loadWave) {
                this.loadVoice()
            }
        })

    },
    methods:{
        // 转化数字为字符串 如果小于0  返回 0x (01 02...)
        buZero(num) {
            return num > 9 ? num : '0' + num
        },
        // 加载或者创建波形图
        loadVoice(bool) {
            if (this.wavesurfer) {
                if (bool) {
                this.time = '00:00'
                this.wavesurfer.load(this.url)
                }
            } else {
                this.wavesurfer = WaveSurfer.create({
                container: this.$refs.waveform,
                waveColor: '#65da74',
                progressColor: 'purple',
                height: '80',
                backgroundColor:"#f7f3f4",
                scrollParent: true,
                plugins: [
                        CursorPlugin.create({
                            showTime: true,
                            opacity: 1,
                            customShowTimeStyle: {
                                'background-color': '#000',
                                color: '#fff',
                                padding: '2px',
                                'font-size': '10px'
                            }
                        })
                    ]
                })
                this.wavesurfer.load(this.url)
                this.wavesurfer.on('audioprocess', (e) => {
                    const times = Math.ceil(e)
                    this.time = this.buZero(Math.floor(times / 60)) + ':' + this.buZero(times % 60)
                })
                this.wavesurfer.on('finish', () => {
                    this.isPlaying = false
                })
            }
        },
        // 开始
        playVoice() {
            if (this.wavesurfer) {
                if (this.wavesurfer.isPlaying()) {
                    this.isPlaying = false
                    this.wavesurfer.pause()
                } else {
                    this.isPlaying = true
                    this.wavesurfer.play()
                }
            }
        }
 
    },
    watch: {
        'loadWave': function() {
            this.playVoice()
        },
        'url': function() {
            this.playVoice(true)
        },
        'toStopVoice': function() {
            if (this.wavesurfer) {
                this.wavesurfer.pause()
            }
        }
    },
}
  • 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
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • style
    .voice-dialog-content{
        display: flex;
        align-items: center;
    }
    #waveform{
        margin-left: 4px;
        margin-bottom: 4px;
        width: 98%;
    }
    .voice-dialog-time{
        margin-left: 3px;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 运行效果
    在这里插入图片描述
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/130168
推荐阅读
相关标签