0
点赞
收藏
分享

微信扫一扫

Uniapp录音实时回调原生插件-YL-AudioRecorder


YL-AudioRecorder 插件地址

升级版 YL-AudioRecorderPlus 支持mp3录制及实时回调

插件说明:

由于uni提供的录音管理器uni.getRecorderManager(),不支持APP上的实时回调,由此便诞生了该原生录音插件。

该录音插件支持PCM录音实时帧回调,支持获取录音文件路径,支持pcm转wav,支持pcm播放,

支持设置录音参数如采样率,声道,位深等;

需要注意的是,目前该插件仅支持安卓端,不支持IOS!

1.使用方法:

  • 1.引入插件:

const ar = uni.requireNativePlugin("YL-AudioRecorder");

  • 2.设置参数

//第1个参数sampleRate:采样率,默认16000
//第2个参数channel:声道:默认1,可选值1或2
//第3个参数audioFormat:位宽:默认16,可选8或16
//参数设置为-1时,不处理
ar.setConfig(-1, -1, -1);

  • 3.开始录音(回调中包含两个参数,buffer,isList):

//buffer实时返回的byte[],大小为你设置的frameSize,若需base64编码,可自行转换
//isList是否是最后一帧,最后一帧时buffer为空
ar.start(res => {
	this.data = "" + res.buffer + "," + res.isLast;
});

...

//转base64方法:
toBase64(buffer) {
	var binary = "";
	var bytes = new Uint8Array(buffer);
	var len = bytes.byteLength;
	for (var i = 0; i < len; i++) {
		binary += String.fromCharCode(bytes[i]);
	}
	var base64 = btoa(binary)
	return base64;
}

  • 4.停止录音,返回录音文件路径:

ar.stop(path => {
	this.path = path;
});

  • 5.播放录音

ar.play(this.path);

  • 6.停止播放:

ar.stopPlay();

  • 7.pcm转wav:

ar.pcmToWav(this.path, path => {
	this.path = path;
});

  • 8.销毁:

ar.release();

代码示例:

<template>
	<div style="padding: 20rpx;">
		<button type="primary" @click="start" style="margin-top: 20rpx;">开始录音</button>
		<button type="primary" @click="stop" style="margin-top: 20rpx;">停止录音</button>
		<button type="primary" @click="play" style="margin-top: 20rpx;">播放录音</button>
		<button type="primary" @click="stopPlay" style="margin-top: 20rpx;">停止播放</button>
		<button type="primary" @click="pcmToWav" style="margin-top: 20rpx;">pcm转wav</button>
		<div style="margin-top: 20rpx;">
			<text class="text">实时回调:{{data}}</text>
		</div>
		<div style="margin-top: 20rpx;">
			<text class="text">录音文件路径:{{path}}</text>
		</div>
	</div>
</template>

<script>
	const ar = uni.requireNativePlugin("YL-AudioRecorder");

	export default {
		data() {
			return {
				data: "",
				path: ""
			}
		},
		mounted() {
			//第1个参数sampleRate:采样率,默认16000
			//第2个参数channel:声道:默认1,可选值1或2
			//第3个参数audioFormat:位宽:默认16,可选8或16
			//参数设置为-1时,不处理
			ar.setConfig(-1, -1, -1);
		},
		beforeDestroy() {
			ar.release();
		},
		methods: {
			start() {
				console.log('start');
				ar.start(res => {
					this.data = "" + res.buffer + "," + res.isLast;
				});
			},
			stop() {
				console.log('stop');
				ar.stop(path => {
					this.path = path;
				});
			},
			play() {
				if (this.path) {
					ar.play(this.path);
				}
			},
			stopPlay() {
				ar.stopPlay();
			},
			pcmToWav() {
				if (this.path) {
					ar.pcmToWav(this.path, path => {
						this.path = path;
					});
				};
			},
			toBase64(buffer) {
				var binary = "";
				var bytes = new Uint8Array(buffer);
				var len = bytes.byteLength;
				for (var i = 0; i < len; i++) {
					binary += String.fromCharCode(bytes[i]);
				}
				var base64 = btoa(binary)
				return base64;
			}
		}
	}
</script>

<style>
	.text {
		line-height: 1.5;
		text-align: justify;
		word-wrap: break-word;
	}
</style>


举报

相关推荐

0 条评论