vue使用wavesurfer.js实现双声道同时播放和左、右声道单独播放

vue使用wavesurfer.js实现双声道同时播放和左、右声道单独播放

<el-select v-model="channel" size="mini" @change="handleChangeChannel">
	<el-option
	    v-for="item in channelOptions"
	    :key="item.value"
	    :label="item.label"
	    :value="item.value"
    />
</el-select>

需要定义的变量:

channel: 'both',
channelOptions: [{ id: 1, value: 'left', label: '左声道' }, { id: 1, value: 'right', label: '右声道' }, { id: 3, value: 'both', label: '同时' }],
leftAudioBuffer: null, // 左声道数据
rightAudioBuffer: null, // 右声道数据
nullAudioBuffer: null,
currentAudioBuffer: null
this.wavesurfer.on('ready', function() {
	// 获取声道数据
	_this.currentAudioBuffer = _this.wavesurfer.backend && _this.wavesurfer.backend.buffer
	if (_this.currentAudioBuffer && _this.currentAudioBuffer.numberOfChannels === 2) {
	    _this.nullAudioBuffer = new Float32Array(_this.currentAudioBuffer.length)
	    _this.leftAudioBuffer = new Float32Array(_this.currentAudioBuffer.length)
	    _this.rightAudioBuffer = new Float32Array(_this.currentAudioBuffer.length)
	    _this.currentAudioBuffer.copyFromChannel(_this.leftAudioBuffer, 0, 0)
	    _this.currentAudioBuffer.copyFromChannel(_this.rightAudioBuffer, 1, 0)
	}
})
// 切换左右声道
 handleChangeChannel(val) {
     var left, right
     // 替换声道的数据,即可将声道静音
     if (val === 'left') {
         left = this.leftAudioBuffer
         right = this.nullAudioBuffer
     } else if (val === 'right') {
         left = this.nullAudioBuffer
         right = this.rightAudioBuffer
     } else if (val === 'both') {
         left = this.leftAudioBuffer
         right = this.rightAudioBuffer
     }
     this.currentAudioBuffer.copyToChannel(left, 0, 0)
     this.currentAudioBuffer.copyToChannel(right, 1, 0)
     this.wavesurfer.drawBuffer()
}

效果如下:
在这里插入图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值