CameraContext.startRecord
基础库 2.41.0 开始支持本方法,低版本需做兼容处理,这是一个异步方法。
开始录像。
语法
CameraContext.startRecord(options)
参数说明
options 为 object 类型,属性如下:
属性名 | 类型 | 默认值 | 必填 | 说明 | 最低支持版本 |
---|---|---|---|---|---|
timeoutCallback | function | 否 | 超过 30s 时会结束录像 | 2.41.0 | |
success | function | 否 | 接口调用成功的回调函数 | 2.41.0 | |
fail | function | 否 | 接口调用失败的回调函数 | 2.41.0 | |
complete | function | 否 | 接口调用结束的回调函数 | 2.41.0 |
回调成功
object 类型,属性如下:
属性名 | 类型 | 说明 | 最低支持版本 |
---|---|---|---|
errMsg | string | "startCameraRecord:ok" | 2.41.0 |
回调失败
object 类型,属性如下:
属性名 | 类型 | 说明 | 最低支持版本 |
---|---|---|---|
errMsg | string | "startCameraRecord:fail" + 详细错误信息 | 2.41.0 |
errNo | number | 错误码 | 2.41.0 |
错误说明
errNo | errMsg | 说明 | 最低支持版本 |
---|---|---|---|
10402 | null throwable native exception ${throwable} stack:${stack} | 客户端异常 | 2.41.0 |
21100 | cameraId not found | cameraId 不存在 | 2.41.0 |
21101 | is recording | 正在录制中 | 2.41.0 |
21102 | not allow to invoke at 'scanCode' mode | 扫码模式下不能调用 | 2.41.0 |
代码示例
<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: "请授权相机后重新进入", // 内容
success: (res) => {
this.auth();
},
});
}
},
fail: (err) => {
tt.showModal({
title: "获取授权失败",
content: JSON.stringify(err),
});
},
});
},
auth() {
tt.authorize({
scope: "scope.camera",
success: () => {
tt.showToast({
title: "授权成功",
});
tt.reLaunch({
url: "/pages/index/index", // 指定页面的url
});
},
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);
},
});
},
});
Bug & Tip
- Tip:开发者工具暂不支持此能力,请用真机扫码调试。
点击纠错
该文档是否对你的开发有所帮助?
有帮助
没帮助
该文档是否对你的开发有所帮助?
有帮助
没帮助