RecorderManager.start
收藏我的收藏
基础库 1.0.0 开始支持本方法,这是一个同步方法。
前提条件 | 无 |
业务背景 | 无 |
使用限制 | 无 |
注意事项 | 无 |
支持沙盒 | 否 |
相关教程 | 无 |
语法
RecorderManager.start(params)
参数说明
params 类型说明
object 类型,属性如下:
属性名 | 类型 | 默认值 | 必填 | 说明 | 最低支持版本 |
---|---|---|---|---|---|
duration | number | 60000 | 否 | 录音自动完成时长,单位 ms,最大值为 60000 ms | 1.0.0 |
format | enum | aac | 否 | 录音格式。目前支持 pcm、wav、aac。 | 1.0.0 |
sampleRate | enum | 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 的合法值
值 | 说明 | 最低支持版本 |
---|---|---|
aac | 无 | 1.0.0 |
pcm | 无 | 1.0.0 |
wav | 无 | 1.0.0 |
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 |
返回值
无
扫码体验
请使用字节宿主APP扫码
代码示例
<view class="container"> <view class="body"> <text class="text-space">剩余录音时间 {{ cd }}ms</text> <view class="btn-area"> <button type="primary" bindtap="start" tt:if="{{ !isStart }}">start</button> <button bindtap="stop" tt:else>stop</button> </view> </view> </view>
const countdown = 6000; let cdtimer = null; Page({ data: { cd: countdown, isStart: false, options: { duration: countdown, sampleRate: 12000, numberOfChannels: 1, encodeBitRate: 25000, frameSize: 100, } }, onLoad() { this.recorderManager = tt.getRecorderManager(); this.recorderManager.onStart(() => { this.setData({ isStart: true, cd: countdown }); this.startCountDown(); }); // 监听录音停止事件 this.recorderManager.onStop((res) => { clearInterval(cdtimer); this.setData({ isStart: false, cd: countdown }); }); }, onUnload: function () { this.stop(); }, start() { this.recorderManager.start(this.data.options); }, stop() { // 停止录音 this.recorderManager.stop(); }, startCountDown() { clearInterval(cdtimer); cdtimer = setInterval(() => { this.setData({ cd: this.data.cd - 100 }); }, 100); } })
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 |