# uni.chooseVideo(options)

拍摄视频或从手机相册中选视频,返回视频的临时文件路径。

# chooseVideo 兼容性

Web 微信小程序 Android iOS HarmonyOS HarmonyOS(Vapor)
4.0 4.41 4.18 4.18 4.61 5.0

# 参数

名称 类型 必填 默认值 兼容性 描述
options ChooseVideoOptions -
-
名称 类型 必备 默认值 兼容性 描述
pageOrientation string -
屏幕方向。默认为page.json中的pageOrientation。
合法值 兼容性 描述
auto
-
自动
portrait
-
竖屏显示
landscape
-
横屏显示
sourceType Array<string> -
album 从相册选视频,camera 使用相机拍摄,默认为:['album', 'camera']
maxDuration number -
拍摄视频最长拍摄时间,单位秒。最长支持 60 秒
camera string -
摄像切换
合法值 兼容性 描述
front
-
前置摄像头
back
-
后置摄像头
extension Array<string> -
根据文件拓展名过滤,每一项都不能是空字符串。默认不过滤。
success (callback: ChooseVideoSuccess) => void -
接口调用成功,返回视频文件的临时文件路径,详见返回参数说明
fail (callback: ChooseVideoFail) => void -
接口调用失败的回调函数
complete (callback: any) => void -
接口调用结束的回调函数(调用成功、失败都会执行)
compressed boolean true
是否压缩所选的视频源文件,默认值为true,需要压缩 已废弃,仅为了向下兼容保留
albumMode string "custom"
视频选择模式 已废弃,仅为了向下兼容保留
合法值 兼容性 描述
custom
-
自定义媒体选择器
system
-
系统媒体选择器

# ChooseVideoSuccess 的属性值

名称 类型 必备 默认值 兼容性 描述
tempFilePath string -
选定视频的临时文件路径
duration number -
选定视频的时间长度
size number -
选定视频的数据量大小
height number -
返回选定视频的长
width number -
返回选定视频的宽

# ChooseVideoFail 的属性值

名称 类型 必备 默认值 兼容性 描述
errCode number -
-
错误码
合法值 兼容性 描述
1101001
-
用户取消
1101002
-
urls至少包含一张图片地址
1101003
-
文件不存在
1101004
-
图片加载失败
1101005
-
未获取权限
1101006
-
图片或视频保存失败
1101007
-
图片裁剪失败
1101008
-
拍照或录像失败
1101009
-
图片压缩失败
1101010
-
其他错误
errSubject string -
-
统一错误主题(模块)名称
data any -
-
错误信息中包含的数据
cause Error -
-
源错误信息,可以包含多个错误,详见SourceError
errMsg string -
-

# 参见

# 示例

示例为hello uni-app x alpha分支,与最新HBuilderX Alpha版同步。与最新正式版同步的master分支示例另见

扫码体验(手机浏览器跳转到App直达页)

示例

<template>
  <!-- #ifdef APP -->
  <scroll-view style="flex:1">
  <!-- #endif -->
    <page-head :title="title"></page-head>
    <view class="uni-padding-wrap">
      <video class="video" :src="src" :controls="true" :poster="videoCoverImage"></video>
      <view class="uni-title">
        <text class="uni-subtitle-text">视频信息</text>
      </view>
      <text>{{videoInfo}}</text>
      <view class="uni-btn-v">
        <button type="primary" @click="chooseVideo">选取视频</button>
      </view>
      <enum-data title="视频来源" :items="sourceTypeItemTypes" @change="onSourceTypeChange"></enum-data>
      <!-- #ifdef APP -->
      <enum-data title="屏幕方向" :items="orientationTypeItemTypes" @change="onOrientationTypeChange"></enum-data>
      <!-- #endif -->
      <enum-data title="摄像头" :items="cameraItemTypes" @change="onCameraChange"></enum-data>
      <!-- #ifdef APP-ANDROID -->
      <enum-data title="相册模式" :items="albumModeTypes" @change="onAlbumModeChange"></enum-data>
      <!-- #endif -->
    </view>
    <input-data title="最长拍摄时间,单位秒" defaultValue="60" type="number" @confirm="onMaxDurationConfirm"></input-data>
    <!-- #ifdef APP -->
    <view class="uni-padding-wrap">
      <boolean-data title="是否压缩(HamonyOS 不支持,推荐使用 uni.compressVideo 进行压缩)" :defaultValue="true" @change="onCompressedChange"></boolean-data>
    </view>
    <!-- #endif -->
  <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
</template>

<script setup lang="uts">
  import { ItemType } from '@/components/enum-data/enum-data-types';
  type Camera = "back" | "front"
  type Source = "album" | "camera"

  const title = ref("chooseVideo")
  const src = ref("")
  const orientationTypeItemTypes = ref([{ "value": 0, "name": "竖屏" }, { "value": 1, "name": "横屏" }, { "value": 2, "name": "自动" }] as ItemType[])
  const sourceTypeItemTypes = ref([{ "value": 0, "name": "从相册中选择视频" }, { "value": 1, "name": "拍摄视频" }, { "value": 2, "name": "从相册中选择视频或拍摄视频" }] as ItemType[])
  const sourceTypeItems = ref([["album"], ["camera"], ["album", "camera"]] as Source[][])
  const cameraItemTypes = ref([{ "value": 0, "name": "后置摄像头" }, { "value": 1, "name": "前置摄像头" }] as ItemType[])
  const albumModeTypes = ref([{ "value": 0, "name": "自定义视频选择器" }, { "value": 1, "name": "系统视频选择器" }] as ItemType[])
  const albumModeTypeItems = ref(["custom", "system"])
  const cameraItems = ref(["back", "front"] as Camera[])
  const sourceType = ref(["album", "camera"] as Source[])
  const orientationType = ref("portrait")
  const orientationTypeItems = ref(["portrait", "landscape", "auto"])
  const compressed = ref(true)
  const maxDuration = ref(60)
  const camera = ref("back" as Camera)
  const videoInfo = ref("")
  const videoCoverImage = ref("")
  const albumMode = ref("custom")

  onPageHide(() => {
    console.log("Page Hide");
  })

  const chooseVideo = () => {
    uni.chooseVideo({
      sourceType: sourceType.value,
      // #ifdef APP
      compressed: compressed.value,
      pageOrientation: orientationType.value,
      // #endif
      maxDuration: maxDuration.value,
      // #ifdef APP-ANDROID
      albumMode: albumMode.value,
      // #endif
      camera: camera.value,
      success: (res) => {
        console.log("chooseVideo success", JSON.stringify(res));
        src.value = res.tempFilePath;
        videoInfo.value = `视频长度: ${res.duration}s\n视频大小: ${Math.ceil(res.size)}KB\n视频宽度: ${res.width}\n视频高度: ${res.height}\n`;
        // #ifdef APP-ANDROID || APP-IOS
        uni.getVideoInfo({
          src: res.tempFilePath,
          success: (_res) => {
            if(_res.thumbTempFilePath != null) {
            videoCoverImage.value = _res.thumbTempFilePath!
            }
          }
        });
        // #endif
      },
      fail: (err) => {
        uni.showModal({
          title: "选择视频失败",
          content: JSON.stringify(err),
          showCancel: false
        });
      }
    });
  }

  const onOrientationTypeChange = (value: number) => {
    orientationType.value = orientationTypeItems.value[value];
  }

  const onSourceTypeChange = (value: number) => {
    sourceType.value = sourceTypeItems.value[value];
  }

  const onCompressedChange = (value: boolean) => {
    compressed.value = value;
  }

  const onMaxDurationConfirm = (value: number) => {
    maxDuration.value = value;
  }

  const onCameraChange = (value: number) => {
    camera.value = cameraItems.value[value];
  }

  const onAlbumModeChange = (value: number) => {
    albumMode.value = albumModeTypeItems.value[value]
  }
</script>

<style>
  .video {
    align-self: center;
    width: 300px;
    height: 225px;
  }
</style>

# 通用类型

# GeneralCallbackResult

名称 类型 必备 默认值 兼容性 描述
errMsg string -
错误信息

# Tips

  • 视频选择的相册,在入参option中,推荐设置albumMode为system,即选择系统相册来选择视频。这样可以避免权限问题,并且google play上架也有要求。google play 照片和视频权限政策。后续会废弃custom方式。
  • 当设置albumModesystem时,可以正常上架google play。同时需要注意在manifest.json中将<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" /><uses-permission android:name="android.permission.READ_MEDIA_VIDEO" />权限移除。配置方式参考移除Android权限.
  • 系统相册选择界面的主题和国际化,跟随手机rom,而不是跟随app。
  • 不推荐使用本API的压缩参数,大视频选择时,会卡在视频选择页面很久。应该在选择完毕视频后,自行择机(比如上传视频时)调用uni.compressVideo来压缩视频。
  • 本API会自动申请摄像头,如需手动获取app是否拥有摄像头,参考 uni.getAppAuthorizeSetting。(如使用system方式的相册选择,则不需要权限)
  • app端拍摄会在应用沙盒目录的cache目录产生临时文件,位置详见。如需主动删除临时文件,使用uni.getFileSystemManager
  • android端由于系统或ROM的限制,拍照的maxDurationcamera属性在部分手机上不生效。
  • 从HBuilderX4.41版起,uni.chooseVideo在sourceType['album']albumModesystemcompressedtrue时,支持返回Uri地址。
  • 系统视频选择器的sizeType仅支持设置['original']['compressed']。在Android 11及以上的系统中,设置system调用的是系统的视频选择器,低于android 11的系统中会调用系统的文件选择器。