# wx.chooseMedia(Object object)

基础库 2.10.0 开始支持,低版本需做兼容处理

Promise 风格 调用:支持

小程序插件:支持,需要小程序基础库版本不低于 2.11.1

微信 Windows 版:支持

微信 Mac 版:支持

# 功能描述

拍摄或从手机相册中选择图片或视频。

# 参数

# Object object

属性 类型 默认值 必填 说明
count number 9 最多可以选择的文件个数,基础库2.25.0前,最多可支持9个文件,2.25.0及以后最多可支持20个文件
mediaType Array.<string> ['image', 'video'] 文件类型
合法值 说明
image 只能拍摄图片或从相册选择图片
video 只能拍摄视频或从相册选择视频
mix 可同时选择图片和视频
sourceType Array.<string> ['album', 'camera'] 图片和视频选择的来源
合法值 说明
album 从相册选择
camera 使用相机拍摄
maxDuration number 10 拍摄视频最长拍摄时间,单位秒。时间范围为 3s 至 60s 之间。不限制相册。
sizeType Array.<string> ['original', 'compressed'] 是否压缩所选文件,基础库2.25.0前仅对 mediaType 为 image 时有效,2.25.0及以后对全量 mediaType 有效
camera string 'back' 仅在 sourceType 为 camera 时生效,使用前置或后置摄像头
合法值 说明
back 使用后置摄像头
front 使用前置摄像头
success function 接口调用成功的回调函数
fail function 接口调用失败的回调函数
complete function 接口调用结束的回调函数(调用成功、失败都会执行)

# object.success 回调函数

# 参数
# Object res
属性 类型 说明
tempFiles Array.<Object> 本地临时文件列表
结构属性 类型 说明
tempFilePath string 本地临时文件路径 (本地路径)
size number 本地临时文件大小,单位 B
duration number 视频的时间长度
height number 视频的高度
width number 视频的宽度
thumbTempFilePath string 视频缩略图临时文件路径
fileType string 文件类型
合法值 说明
image 图片
video 视频
type string 文件类型,有效值有 image 、video、mix

# 示例代码

wx.chooseMedia({
  count: 9,
  mediaType: ['image','video'],
  sourceType: ['album', 'camera'],
  maxDuration: 30,
  camera: 'back',
  success(res) {
    console.log(res.tempFiles.tempFilePath)
    console.log(res.tempFiles.size)
  }
})