CameraContext.stopRecord
收藏
我的收藏

基础库 2.41.0 开始支持本方法,低版本需做兼容处理,这是一个异步方法。

结束录像。

前提条件
业务背景
使用限制
注意事项
Tip:开发者工具暂不支持此能力,请用真机扫码调试。
支持沙盒
相关教程

语法

CameraContext.stopRecord(options)

参数说明

options 为 object 类型,属性如下:

属性名类型默认值必填说明最低支持版本
compressedboolean
启动视频压缩,压缩效果同 chooseVideo,需要压缩,Android 暂不支持
2.41.0
successfunction
接口调用成功的回调函数
2.41.0
failfunction
接口调用失败的回调函数
2.41.0
completefunction
接口调用结束的回调函数(调用成功、失败都会执行)
2.41.0

回调成功

object 类型,属性如下:

属性名类型说明最低支持版本
errMsgstring
"CameraContext.stopRecord:ok"
2.41.0
durationnumber
视频时长(单位:ms)
2.41.0
widthstring
视频宽度(单位:px)
2.41.0
heightstring
视频高度(单位:px)
2.41.0
sizenumber
本地文件大小(单位:byte)
2.41.0
tempThumbPathstring
封面图片文件的临时路径 (本地路径),jpeg 格式
2.41.0
tempVideoPathstring
视频的文件的临时路径 (本地路径),mp4 格式
2.41.0

回调失败

object 类型,属性如下:

属性名类型说明最低支持版本
errMsgstring
"CameraContext.stopRecord:fail" + 详细错误信息
2.41.0

errMsg 参数说明

errNo

errMsg

说明

最低支持版本

10402

null throwable native exception ${throwable} stack:${stack}

客户端异常

2.41.0

21100

cameraId not found

cameraId 不存在

2.41.0

21101

is not recording

不在录制中

2.41.0

21102

not allow to invoke at 'scanCode' mode

扫码模式下不能调用

2.41.0

扫码体验

请使用字节宿主APP扫码

代码示例

开发者工具中预览

<camera flash="off" style="width: 100%; height: 400rpx;"></camera>
<button type="primary" bindtap="startRecord">开始录像</button>
<button type="primary" bindtap="stopRecord">停止录像</button>
<view style="text-align: center; font-size: 30rpx; padding-top: 20rpx;">
  录像结果
</view>
<video src="{{src}}" autoplay="{{true}}" style="width: 100%; height: 400rpx;"></video>
Page({
  data: {
    src: "",
  },
  onLoad: function (options) {
    tt.getSetting({
      success: (res) => {
        let cameraAllowed = res.authSetting["scope.camera"];
        if (!cameraAllowed) {
          tt.showToast({
            title: "请授权相机后重新进入"
          });
        }
      },
      fail: (err) => {
        tt.showModal({
          title: "获取授权失败",
          content: JSON.stringify(err),
        });
      },
    });
  },
  startRecord() {
    if (!this.ctx) {
      this.ctx = tt.createCameraContext();
    }
    this.ctx.startRecord({
      timeoutCallback: (res) => {
        console.log("timeoutCallback", res);
        this.setData({
          src: res.tempVideoPath,
        });
      },
      success: (res) => {
        console.log("success", res);
      },
      fail(err) {
        console.log("fail", err);
      },
    });
  },
  stopRecord() {
    if (!this.ctx) {
      this.ctx = tt.createCameraContext();
    }
    this.ctx.stopRecord({
      compressed: true,
      success: (res) => {
        console.log("success", res);
        this.setData({
          src: res.tempVideoPath,
        });
      },
      fail(err) {
        console.log("fail", err);
      },
    });
  },
});