RecorderManager.onStart
收藏
我的收藏

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

监听录音开始事件。

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

语法

RecorderManager.onStart(callback)

参数说明

callback

类型默认值必填说明最低支持版本
function
监听录音开始事件的回调函数
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" disabled="{{ isStart }}">start</button>
      <button type="primary" bindtap="pause" tt:if="{{ isPlay }}">pause</button>
      <button type="primary" bindtap="resume" tt:if="{{ !isPlay }}" disabled="{{ !isStart }}">resume</button>
      <button type="primary" bindtap="stop" disabled="{{ !isStart }}">stop</button>
    </view>
  </view>
</view>
const countdown = 6000;
let cdtimer = null;
Page({
  data: {
    cd: countdown,
    isStart: false,
    isPlay: false,
    options: {
      duration: countdown,
      sampleRate: 12000,
      numberOfChannels: 1,
      encodeBitRate: 25000,
      frameSize: 100,
    }
  },
  onLoad() {
    this.recorderManager = tt.getRecorderManager();
    // 监听录音开始事件
    this.recorderManager.onStart(() => {
      this.setData({
        isStart: true,
        isPlay: true,
        cd: countdown
      });
      this.startCountDown();
      tt.showToast({
        title: '录音开始了'
      });
    });
    // 监听录音暂停事件
    this.recorderManager.onPause(() => {
      this.setData({
        isPlay: false
      });
      clearInterval(cdtimer);
      console.log("已暂停录音");
    });
    // 监听录音继续事件
    this.recorderManager.onResume(() => {
      this.setData({
        isPlay: true
      });
      this.startCountDown();
      console.log("已继续录音");
    });
    // 监听录音停止事件
    this.recorderManager.onStop((res) => {
      clearInterval(cdtimer);
      this.setData({
        isStart: false,
        isPlay: false,
        cd: countdown
      });
      console.log("已停止录音,录音文件的地址为: ", res.tempFilePath);
    });
  },
  onUnload: function () {
    this.stop();
  },
  start() {
    this.recorderManager.start(this.data.options);
  },
  stop() {
    // 停止录音
    this.recorderManager.stop();
  },
  pause() {
    // 暂停录音
    this.recorderManager.pause();
  },
  resume() {
    // 继续录音
    this.recorderManager.resume();
  },
  startCountDown() {
    clearInterval(cdtimer);
    cdtimer = setInterval(() => {
      this.setData({
        cd: this.data.cd - 100
      });
    }, 100);
  }
})