mc.chooseMedia(Object object)
拍摄或从手机相册中选择图片或视频。
参数
Object object
| 属性 | 类型 | 默认值 | 必填 | 说明 |
|---|---|---|---|---|
| count | Number | 9 | 否 | 最多可以选择的文件个数 |
| mediaType | [String] | ['image', 'video'] | 否 | 文件类型 |
| sourceType | [String] | ['album', 'camera'] | 否 | 图片和视频选择的来源 |
| maxDuration | Number | 60 | 否 | 最长拍摄时间或相册视频时长,单位秒。 |
| sizeType | [String] | ['original', 'compressed'] | 否 | 仅对 mediaType 为 image 时有效,是否压缩所选文件 |
| camera | String | 'back' | 否 | 仅在 sourceType 为 camera 时生效,使用前置或后置摄像头 |
| success | Function | 否 | 接口调用成功的回调函数 | |
| fail | Function | 否 | 接口调用失败的回调函数 | |
| complete | Function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
Object.mediaType 的合法值
| 值 | 说明 | 最低版本 |
|---|---|---|
| image | 只能拍摄图片或从相册选择图片 | |
| video | 只能拍摄视频或从相册选择视频 |
Object.sourceType 的合法值
| 值 | 说明 | 最低版本 |
|---|---|---|
| album | 从相册选择 | |
| camera | 使用相机拍摄 |
Object.camera 的合法值
| 值 | 说明 | 最低版本 |
|---|---|---|
| back | 使用后置摄像头 | |
| front | 使用前置摄像头 |
Object.success(Object res) 回调函数
参数
Object res
| 属性 | 类型 | 说明 |
|---|---|---|
| tempFiles | [Object] | 本地临时文件列表 |
| type | String | 文件类型,有效值有 image 、video |
res.tempFiles 的结构
| 属性 | 类型 | 说明 |
|---|---|---|
| tempFilePath | String | 本地临时文件路径 (本地路径) |
| size | Number | 本地临时文件大小,单位 B |
| duration | Number | 视频的时间长度 |
| height | Number | 视频的高度 |
| width | Number | 视频的宽度 |
| thumbTempFilePath | String | 视频缩略图临时文件路径 |
示例代码:
chooseMedia: function () {
var _this = this;
mc.chooseMedia({
count: 9,
mediaType: ['image', 'video'],
sourceType: ['album', 'camera'],
maxDuration: 30,
sizeType: ['original', 'compressed'],
camera: 'back',
complete(res) {
_this.setData({
chooseMediaInfo: JSON.stringify(res, null, 2)
})
}
})
}