

English
Get the globally unique recording manager recorderManager
.
Platform Difference Description
App | H5 | 微信小程序 | 支付宝小程序 | 百度小程序 | 抖音小程序、飞书小程序 | QQ小程序 | 快手小程序 | 京东小程序 | 元服务 | 小红书小程序 |
---|---|---|---|---|---|---|---|---|---|---|
√ | x | √ | √ | √ | √ | √ | √ | √ | x | x |
HarmonyOS |
---|
HBuilderX 4.31 |
List of methods of recorderManager object
Method | Parameter | Description | Platform Difference Description |
---|---|---|---|
start | options | Start recording | |
pause | pause recording | App not supported at the moment | |
resume | Continue recording | App not supported yet | |
stop | 停止录音 | ||
offStop | callback | 取消监听录音停止事件 | 仅支付宝小程序支持 |
onStart | callback | 录音开始事件 | |
offStart | callback | 移除录音开始事件 | 仅支付宝小程序支持 |
onPause | callback | 录音暂停事件 | |
offPause | callback | 移除监听录音暂停事件 | 仅支付宝小程序支持 |
onStop | callback | 录音停止事件,会回调文件地址 | |
onResume | callback | 监听录音继续事件 | 仅小程序支持 |
offResume | callback | 取消监听录音继续事件 | 仅支付宝小程序支持 |
onInterruptionBegin | callback | 监听录音因为受到系统占用而被中断开始事件。以下场景会触发此事件:微信语音聊天、微信视频聊天、QQ语音聊天、QQ视频聊天、电话响铃、接听电话。此事件触发后,录音会被暂停。pause 事件在此事件后触发 | 微信小程序、百度小程序、QQ小程序、快手小程序 |
onInterruptionEnd | callback | 监听录音中断结束事件。在收到 interruptionBegin 事件之后,小程序内所有录音会暂停,收到此事件之后才可再次录音成功。 | 微信小程序、百度小程序、QQ小程序、快手小程序 |
onFrameRecorded | callback | 已录制完指定帧大小的文件,会回调录音分片结果数据。如果设置了 frameSize ,则会回调此事件 | App 暂不支持 |
offFrameRecorded | callback | 取消监听已录制完指定帧大小的文件事件,指定 frameSize 大小并且 format 参数设置为 mp3 格式,调用此接口才会有回调 | 仅支付宝小程序支持 |
onDecibelChange | callback | 监听声音分贝变化事件,详见 | 仅支付宝小程序支持 |
offDecibelChange | callback | 取消监听声音分贝变化事件,详见 | 仅支付宝小程序支持 |
onError | callback | 录音错误事件, 会回调错误信息 | |
offError | callback | 取消监听录音错误事件 | 仅支付宝小程序支持 |
Attribute | Type | Required | Description | Platform Difference Description |
---|---|---|---|---|
duration | Number | No | Specify the duration of the recording, the unit is ms. If a valid duration is passed in, the recording will stop automatically after reaching the specified duration. The maximum value is 600000 (10 minutes), and the default value is 60000 (1 minute) | App, MiniApp support |
sampleRate | Number | No | Sampling rate, effective value 8000/16000/44100 | App, MiniApp support |
numberOfChannels | Number | No | Number of recording channels, valid value 1/2 | Only supported by MiniApp |
encodeBitRate | Number | No | Encode bit rate, see the table below for valid values | Only supported by MiniApp |
format | String | No | Audio format, valid values are aac/mp3/wav/PCM. The default value of the app is mp3, and the default value of the MiniApp is aac | App, MiniApp support |
frameSize | String | 否 | 指定帧大小,单位 KB。传入 frameSize 后,每录制指定帧大小的内容后,会回调录制的文件内容,不指定则不会回调。暂仅支持 mp3 格式。 | App、百度小程序不支持 |
hideTips | Boolean | 否 | 隐藏录音图标。 | 支付宝小程序10.1.85+ |
audioSource | String | 否 | 指定录音的音频输入源。 | 微信小程序详见、支付宝小程序详见、百度小程序详见、快手小程序 |
detectDecibel | Boolean | 否 | 检测声音分贝数。详见 | 支付宝小程序10.2.0+ |
编码格式与采样率、码率的关系
Android、iOS、微信小程序
Sampling rate | Coding rate |
---|---|
8000 | 16000 - 48000 |
11025 | 16000 - 48000 |
12000 | 24000 - 64000 |
16000 | 24000 - 96000 |
22050 | 32000 - 128000 |
24000 | 32000 - 128000 |
32000 | 48000 - 192000 |
44100 | 64000 - 320000 |
48000 | 64000 - 320000 |
HarmonyOS
Attribute | Type | Description |
---|---|---|
tempFilePath | String | 录音文件的临时路径 |
duration | Number | 录音总时长。单位:s。(仅支付宝10.2.90+支持) |
fileSize | Number | 录音文件大小。单位:Byte。(仅支付宝10.2.90+支持) |
Attribute | Type | Description |
---|---|---|
frameBuffer | ArrayBuffer | recording fragmentation result data |
isLastFrame | Boolean | Whether the current frame is the last frame before the end of normal recording |
Attribute | Type | Description |
---|---|---|
errMsg | String | error message |
注意
HarmonyOS Next
平台使用时需要添加权限 ohos.permission.MICROPHONE
<template>
<view>
<button @tap="startRecord">开始录音</button>
<button @tap="endRecord">停止录音</button>
<button @tap="playVoice">播放录音</button>
</view>
</template>
const recorderManager = uni.getRecorderManager();
const innerAudioContext = uni.createInnerAudioContext();
innerAudioContext.autoplay = true;
export default {
data() {
return {
text: 'uni-app',
voicePath: ''
}
},
onLoad() {
let self = this;
recorderManager.onStop(function (res) {
console.log('recorder stop' + JSON.stringify(res));
self.voicePath = res.tempFilePath;
});
},
methods: {
startRecord() {
console.log('开始录音');
recorderManager.start();
},
endRecord() {
recorderManager.stop();
},
playVoice() {
console.log('播放录音');
if (this.voicePath) {
innerAudioContext.src = this.voicePath;
innerAudioContext.play();
}
}
}
}