RecorderManager.start
基础库 1.0.0 开始支持本方法,这是一个同步方法。
开始录音。该方法需要用户授权方可调用,详细信息可参考用户授权。
语法
RecorderManager.start(options)
参数说明
options
类型 | 默认值 | 必填 | 说明 | 最低支持版本 |
---|---|---|---|---|
Options | 否 | 进入全屏的请求参数 | 1.44.0 |
Options 类型说明
object 类型,属性如下:
属性名 | 类型 | 默认值 | 必填 | 说明 | 最低支持版本 |
---|---|---|---|---|---|
duration | number | 60000 | 否 | 录音自动完成时长,单位 ms | 1.0.0 |
sampleRate | number | 8000 | 否 | 采样率,详见sampleRate 的合法值 | 1.0.0 |
numberOfChannels | number | 2 | 否 | 录音通道数,详见numberOfChannels 的合法值 | 1.0.0 |
encodeBitRate | number | 48000 | 否 | 码率 | 1.0.0 |
frameSize | number | 8000 | 否 | 帧大小,单位 KB。如果设置了值,那么每当录音内容达到帧大小时会通过 onFrameRecorded 返回内容 | 1.0.0 |
format | string | aac | 否 | 录音格式。目前支持 pcm、wav、aac。 | 2.26.0 |
sampleRate 的合法值
值 | 说明 | 最低支持版本 |
---|---|---|
8000 | 8000 采样率 | 1.0.0 |
11025 | 11025 采样率 | 1.0.0 |
12000 | 12000 采样率 | 1.0.0 |
16000 | 16000 采样率 | 1.0.0 |
22050 | 22050 采样率 | 1.0.0 |
24000 | 24000 采样率 | 1.0.0 |
32000 | 32000 采样率 | 1.0.0 |
44100 | 44100 采样率 | 1.0.0 |
48000 | 48000 采样率 | 1.0.0 |
numberOfChannels 的合法值
值 | 说明 | 最低支持版本 |
---|---|---|
1 | 1 个通道 | 1.0.0 |
2 | 2 个通道 | 1.0.0 |
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 |
返回值
无
扫码体验
请使用字节宿主APP扫码
代码示例
【示例 1】无参数示例:
<!-- index.ttml -->
<button type="primary" bindtap="startRecord">startRecord(无参数)</button>
// index.js
Page({
startRecord() {
const recorderManager = tt.getRecorderManager();
recorderManager.start(); // 所有参数为默认值
tt.showToast({ title: "点击了开始录音,参数为默认值" });
},
});
【示例 2】不设置 frameSize 参数示例:
<!-- index.ttml -->
<button type="primary" bindtap="startRecord">startRecord</button>
// index.js
Page({
startRecord() {
const recorderManager = tt.getRecorderManager();
const options = {
duration: 1000,
sampleRate: 12000,
numberOfChannels: 1,
encodeBitRate: 25000,
frameSize: 100,
};
recorderManager.start(options);
tt.showToast({ title: "点击了开始录音" });
},
});
【示例 3】设置 frameSize 示例:
<!-- index.ttml -->
<button type="primary" bindtap="startRecord">startRecord</button>
<view tt:if="{{frameNum > 0}}">当前接收到的帧数:{{frameNum}}</view>
// index.js
Page({
data: {
frameNum: 0,
},
startRecord() {
const recorderManager = tt.getRecorderManager();
const options = {
duration: 60000,
sampleRate: 12000,
numberOfChannels: 1,
encodeBitRate: 25000,
frameSize: 100,
};
recorderManager.start(options);
tt.showToast({ title: "点击了开始录音" });
// 达到指定帧大小后在 `onFrameRecorded` 回调中输出帧数据,
recorderManager.onFrameRecorded((frame) => {
// 若指定帧大小大于整条语音,则在最后一帧输出,可根据 `isLastFrame` 判断是否是最后一帧。
let { isLastFrame } = frame;
this.setData({
frameNum: this.data.frameNum + 1,
});
isLastFrame && tt.showToast({ title: "录音结束" });
});
},
});
Bug & Tip
无
点击纠错
该文档是否对你的开发有所帮助?
有帮助
没帮助
该文档是否对你的开发有所帮助?
有帮助
没帮助