RecorderManager.start
收藏我的收藏
基础库 1.0.0 开始支持本方法,这是一个同步方法。
开始录音。
前提条件 | 该方法需要用户授权方可调用,详细信息可参考用户授权。 |
业务背景 | 无 |
使用限制 | 无 |
注意事项 | 无 |
相关教程 | 无 |
语法
RecorderManager.start(params)
参数说明
params 类型说明
object 类型,属性如下:
属性名 | 类型 | 默认值 | 必填 | 说明 | 最低支持版本 |
---|---|---|---|---|---|
duration | number | 60000 | 否 | 录音自动完成时长,单位 ms | 1.0.0 |
sampleRate | enum | 8000 | 否 | 采样率 | 1.0.0 |
numberOfChannels | number | 2 | 否 | 录音通道数 | 1.0.0 |
encodeBitRate | number | 48000 | 否 | 码率 | 1.0.0 |
frameSize | number | 8000 | 否 | 帧大小,单位 KB。如果设置了值,那么每当录音内容达到帧大小时会通过 RecorderManager.onFrameRecorded 返回内容 | 1.0.0 |
numberOfChannels 参数说明
值 | 说明 |
1 | 1 个通道 |
2 | 2 个通道 |
encodeBitRate 参数说明
每种采样率(sampleRate)有对应的编码码率(encodeBitRate)范围有效值,设置不合法的采样率或编码码率会导致录音失败,具体对应关系如下表:
采样率 | 编码码率 |
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 |
sampleRate 的合法值
值 | 说明 | 最低支持版本 |
---|---|---|
16000 | 16000 采样率 | 1.0.0 |
48000 | 48000 采样率
| 1.0.0 |
24000 | 24000 采样率
| 1.0.0 |
32000 | 32000 采样率
| 1.0.0 |
8000 | 8000 采样率
| 1.0.0 |
11025 | 11025 采样率
| 1.0.0 |
12000 | 12000 采样率
| 1.0.0 |
22050 | 22050 采样率
| 1.0.0 |
44100 | 44100 采样率
| 1.0.0 |
返回值
无
错误码
errNo | errMsg | 说明 | 最低支持版本 |
---|---|---|---|
20000 | Operation type is null | 1.0.0 | |
21000 | Operation type is error | 1.0.0 | |
21501 | invalid encodeBitRate ${encodeBitRate} , encodeBitRate should be greater than ${minRate} and less than ${maxRate} | 1.0.0 | |
20000 | internal error | 小游戏框架内部错误,有需要请创建工单咨询 | 1.0.0 |
代码示例
【代码示例1】:无输入参数。
预期表现:正常启动录音,录音时长等参数为默认值。
const recorderManager = tt.getRecorderManager(); recorderManager.start(); // 所有参数为默认值 tt.showToast({ title: "点击了开始录音,参数为默认值" });
【代码示例2】:不设置 frameSize 参数示例
预期表现:正常启动录音,无 onFrameRecorded 回调。
const recorderManager = tt.getRecorderManager(); const options = { duration: 1000, sampleRate: 12000, numberOfChannels: 1, encodeBitRate: 25000, frameSize: 100, }; recorderManager.start(options); tt.showToast({ title: "点击了开始录音" });
【代码示例 3】:设置 frameSize 示例
预期表现:正常启动录音,达到指定帧大小后在 onFrameRecorded 回调中输出帧数据,若指定帧大小大于整条语音,则在最后一帧输出,可根据 isLastFrame 判断是否是最后一帧。
const recorderManager = tt.getRecorderManager(); const options = { duration: 60000, sampleRate: 12000, numberOfChannels: 1, encodeBitRate: 25000, frameSize: 100, }; recorderManager.start(options); tt.showToast({ title: "点击了开始录音" }); recorderManager.onFrameRecorded((frame) => { if (frame.isLastFrame) { tt.showToast({ title: "录音结束" }); } });