vue+wavesurfer.js:画波形图

wavesurfer.js的信息
 // git
 https://github.com/katspaugh/wavesurfer.js
 // 官网
 https://wavesurfer-js.org/docs/events.html
安装
yarn add wavesurfer.js
使用
  • 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>
  • 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()
            }
        }
    },
}
  • style
    .voice-dialog-content{
        display: flex;
        align-items: center;
    }
    #waveform{
        margin-left: 4px;
        margin-bottom: 4px;
        width: 98%;
    }
    .voice-dialog-time{
        margin-left: 3px;
    }
  • 运行效果
    在这里插入图片描述
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值