RecorderManager.start
收藏
我的收藏

基础库 1.0.0 开始支持本方法,这是一个同步方法。

开始录音。该方法内部会申请录音权限,用户授权后方可录音,详细信息可参考用户授权。若用户拒绝授权,会触发 onError 回调。

前提条件
业务背景
使用限制
注意事项
支持沙盒
相关教程

语法

RecorderManager.start(params)

参数说明

params 类型说明

object 类型,属性如下:

属性名类型默认值必填说明最低支持版本
durationnumber60000

录音自动完成时长,单位 ms,最大值为 60000 ms

1.0.0
formatenumaac

录音格式。目前支持 pcm、wav、aac。

1.0.0
sampleRateenum8000

采样率,详见 sampleRate 的合法值

1.0.0
numberOfChannelsnumber2

录音通道数,详见 numberOfChannels 的合法值

1.0.0
encodeBitRatenumber48000

码率

1.0.0
frameSizenumber8000

帧大小,单位 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​

sampleRateencodeBitRate 的对应关系​

每种采样率有对应的编码码率范围有效值,设置不合法的采样率或编码码率会导致录音失败,具体对应关系如下表:​
采样率​
编码码率​
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​