import Recorder from 'recorder-core'
import 'recorder-core/src/engine/mp3'
import 'recorder-core/src/engine/mp3-engine' //如果此格式有额外的编码引擎（*-engine.js）的话，必须要加上


export function recOpen(recObject = {}, fn) {
	//  recObject添加属性 rec
	let rec = (recObject.rec = Recorder({
		type: recObject.type || 'mp3', // 需要使用的type类型,wav或者mp3
		bitRate: recObject.bitRate || 36, // 比特率kbps，越大音质越好
		sampleRate: recObject.sampleRate || 18000, // 采样率hz，每秒音波震动次数，越大细节越丰富
		onProcess: function(buffers, powerLevel, duration, sampleRate) {
			// 利用waveview扩展实时绘制波形
			if (recObject.wave) recObject.wave.input(buffers[buffers.length - 1], powerLevel,
				sampleRate)
		}, //录音实时回调，大约1秒调用12次回调
	}))
	rec.open(
		function() {
			console.log('打开成功：')
			// recwave：渲染音频波形图的ref名字,3是传一个名字，2是this.$refs【recwave】；非必须
			if (recObject.recwave) {
				recObject.wave = Recorder.WaveView({ elem: recObject.recwave })
			};
			fn('打开成功');
		},
		function(msg, isUserNotAllow) {
			// 浏览器不支持录音、用户拒绝麦克风权限、或者非安全环境（非https、file等
			console.log((isUserNotAllow ? 'UserNotAllow，' : '') + '无法录音:' + msg)
			fn('打开失败')
		}
	)
}