cameracontext.startrecord(object object)
基础库 或更高版本; 若版本较低,建议采取 兼容处理 。
小程序开发者工具: 不支持。请以真机的调试结果为准。
主体: 企业支付宝小程序 、 个人支付宝小程序
开始录像。
object object
查看示例
属性 | 类型 | 默认值 | 必填 | 兼容性 | 描述 |
---|---|---|---|---|---|
needtimestamp | boolean | false | 否 | 客户端: 10.5.90 |
是否在录制完成的视频左上角加时间戳水印。入参值为true时,会在视频左上角加时间戳水印 |
timeout | number | 30 | 否 | 基础库: 2.8.2 |
录制时长上限,最大录制时长为 10 分钟。单位:s |
timeoutcallback | function | - | 否 | - | 超过录制时长上限时会结束录像并触发此回调 |
success | function | - | 否 | - | 调用成功的回调函数 |
fail | function | - | 否 | - | 调用失败的回调函数 |
complete | function | - | 否 | - | 调用结束的回调函数(调用成功、失败都会执行) |
timeoutcallback 回调函数
参数
object res
查看示例
属性 | 类型 | 兼容性 | 描述 |
---|---|---|---|
tempthumbpath | string | - | 封面图片文件地址(本地临时文件)。 |
tempvideopath | string | - | 视频文件地址(本地临时文件)。 |
height | number | 基础库: 2.8.0 支付宝: 10.2.90 |
视频文件的高度。单位:px。 |
width | number | 基础库: 2.8.0 支付宝: 10.2.90 |
视频文件的宽度。单位:px。 |
size | number | 基础库: 2.8.0 支付宝: 10.2.90 |
视频文件的尺寸。单位:bytes。 |
duration | number | 基础库: 2.8.0 支付宝: 10.2.90 |
录制的持续时间,单位为 s。 |
fail 回调的参数为 object,error
属性为错误码,errormessage
属性为错误消息。
错误码 | 错误消息 | 凯发app官方网站的解决方案 |
---|---|---|
7 | 已在录像中。 |
当前已有录像在进行,可在交互设计中考虑这种情况。 |
2001 | 用户不允许授权。 |
用户拒绝为当前小程序授权。 请在交互中考虑此情况。如有需要,请向用户强调该授权的必要性。 |
2002 | 用户拒绝为当前小程序授权,并勾选了“总是保持以上选择”后,再次调用授权接口产生的报错。 |
如有需要,提醒或引导用户手动授权:小程序右上角胶囊按钮->设置->打开权限开关;或者通过调用 my.opensetting 帮助用户打开设置页面。 |
2003 | 用户勾选了不允许授权选项。 |
如有需要,提醒或引导用户手动授权:小程序右上角胶囊按钮->设置->打开权限开关;或者通过调用 my.opensetting 帮助用户打开设置页面。 |
<camera
id="camera"
device-position="front"
flash="off"
style="width: 100%; height: 300px;"
onready="oncameraready"
/>
page ({
oncameraready (e) {
console.log ('相机初始化完成');
this.cameracontext = my.createcameracontext ('camera');
this.cameracontext.startrecord ({
timeout: 10,
timeoutcallback (res) {
console.log ('超时结束录制', res);
},
success: function (res) {
console.log (res);
},
fail: function (err) {
console.log (err);
},
});
},
});
{
"tempthumbpath": "https://resource/719e329e2d8cedb2f9aad42b11da23ec3.image",
"tempvideopath": "https://resource/d2ea29ad59d1550166ac5d7216e0b3c1ii10926c457a1f65d921db2adeeb275ea12.video",
"duration": 9.84000015258789,
"size": 1907337,
"height": 720,
"width": 944
}
this.cameracontext.startrecord ({
timeout: 10,
timeoutcallback (res) {
console.log ('超时结束录制', res);
// 以查视频真实文件扩展名为例
my.detectfiletype ({
filepath: res.tempvideopath,
success (res) {
console.log ('detectfiletype: success', res);
},
fail (err) {
console.log ('detectfiletype: fail', err);
},
});
},
success: function (res) {
console.log (res);
},
fail: function (err) {
console.log (err);
},
});
{
"success": true
}
{
"error": 7,
"errormessage": "已在录像中"
}
-
bug基础库 2.8.19 之前,发生相机意外停止、页面压后台、超出最大时长限制自动结束录像、拒绝为当前小程序授权等情况后,再次直接调用 startrecored() 会报错
{"error":7, "errormessage":"已在录像中"}
,需要先调用一次 stoprecord() 以规避此问题 -
tip建制产生的封面图片和视频文件路径文件扩展名为 .image/.video。若需判断文件实际类型,可使用 my.detectfiletype ,参考上方 示例代码。
-
bug支付宝客户端 10.5.26 版本开始支持无声录制,即不强依赖麦克风权限。
-
tiptimeoutcallback 回调函数参数 duration,支付宝客户端 10.5.20 版本之前,安卓时间单位为毫秒,ios 时间单位为秒,注意版本兼容。