tt.chooseVideo
收藏
我的收藏

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

从系统相册中选择视频,或使用相机拍摄视频,返回视频的临时文件路径。

前提条件
业务背景
使用限制

该 API 需要用户授权方可调用,详细信息可参考用户授权

注意事项
  • Bug: iOS 无论 compressed 传入 true 或者 false, 都会压缩处理;
  • Bug: Android 无论 compressed 传入 true 或者 false, 都不会压缩;
  • Tip:手动取消也会触发失败回调,回调参数为 chooseVideo:fail cancel,可以通过判断详细错误信息区分取消和其他错误;
  • Tip:基础库版本大于等于 2.8.0 时,maxDuration <= 0 时取值 60,无上限限制;
  • Tip:基础库版本低于 2.8.0 时,maxDuration 取值范围 (0, 180],maxDuration <= 0 时取值 60,maxDuration > 180 时取值 180,在取值范围内为传入值。
支持沙盒
相关教程

语法

tt.chooseVideo(options)

参数说明

options 为 object 类型,属性如下:

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

拉起前置摄像头(front)或后置摄像头(back),默认是后置摄像头,Android 暂不支持

1.0.0
sourceTypearray["album","camera"]
指定视频来源为相册(album)或相机(camera) ,默认两者都有
1.0.0
compressedbooleantrue

是否需要压缩视频源文件,默认值为 true,需要压缩,Android 暂不支持

1.0.0
maxDurationnumber60
选择视频的最大时长(单位:s)
1.0.0
successfunction
接口调用成功的回调函数
1.0.0
failfunction
接口调用失败的回调函数
1.0.0
completefunction
接口调用结束的回调函数(调用成功、失败都会执行)
1.0.0

回调成功

object 类型,属性如下:

属性名类型说明最低支持版本
widthnumber
选定视频的视频宽度(单位:px)
1.0.0
heightnumber
选定视频的高度(单位:px)
1.0.0
tempFilePathstring
选定视频的临时文件路径
1.0.0
durationnumber
选定视频的时间长度 (单位:s)
1.0.0
sizenumber
选定视频的数据量大小(单位:B)
1.0.0
errMsgstring
"chooseVideo:ok"
1.0.0

回调失败

object 类型,属性如下:

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

错误码

errNoerrMsg说明最低支持版本
10201"chooseVideo:fail privacy permission is not authorized"

用户拒绝隐私协议授权,详见小程序隐私协议开发指南

3.19.0
10202"chooseVideo:fail api scope is not declared in the privacy agreement"

隐私协议中未定义相关隐私信息类型,详见配置隐私协议

3.19.0

扫码体验

请使用字节宿主APP扫码

代码示例

开发者工具中预览

<!-- index.ttml -->
<button type="primary" bindtap="chooseVideo">选择视频</button>
<view>
  <view>{{description}}</view>
  <video src="{{videoSrc}}" controls="true"></video>
</view>
// index.js
Page({
  data: {
    videoSrc: "",
    description: "",
  },
  chooseVideo() {
    tt.chooseVideo({
      sourceType: ["album", "camera"],
      compressed: true,
      success: (res) => {
        let { duration, width, height, size } = res;
        this.setData({
          videoSrc: res.tempFilePath,
          description: `视频时长:${duration}s; 视频宽度:${width}px; 视频高度:${height}px; 视频大小:${size}B`,
        });
      },
      fail: (err) => {
        let errType = err.errMsg.includes("chooseVideo:fail cancel")
          ? "取消选择"
          : "选择失败";
        tt.showModal({
          title: errType,
          content: err.errMsg,
          showCancel: false,
        });
      },
      complete: () => {
        console.log("完成选择");
      },
    });
  },
});