
简体中文
拍摄视频或从手机相册中选视频,返回视频的临时文件路径。
Web | 微信小程序 | Android | iOS | HarmonyOS |
---|---|---|---|---|
4.0 | 4.41 | 4.18 | 4.18 | 4.61 |
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options | ChooseVideoOptions | 是 | - | - | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
---|---|---|---|---|---|
tempFilePath | string | 是 | - | 选定视频的临时文件路径 | |
duration | number | 是 | - | 选定视频的时间长度 | |
size | number | 是 | - | 选定视频的数据量大小 | |
height | number | 是 | - | 返回选定视频的长 | |
width | number | 是 | - | 返回选定视频的宽 |
名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 | |||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
errCode | number | 是 | - | - | 错误码 | |||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||
errSubject | string | 是 | - | - | 统一错误主题(模块)名称 | |||||||||||||||||||||||||||||||||
data | any | 否 | - | - | 错误信息中包含的数据 | |||||||||||||||||||||||||||||||||
cause | Error | 否 | - | - | 源错误信息,可以包含多个错误,详见SourceError | |||||||||||||||||||||||||||||||||
errMsg | string | 是 | - | - |
示例为hello uni-app x alpha分支,与最新HBuilderX Alpha版同步。与最新正式版同步的master分支示例另见
示例
<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>
import { ItemType } from '@/components/enum-data/enum-data-types';
type Camera = "back" | "front"
type Source = "album" | "camera"
export default {
data() {
return {
title: "chooseVideo",
src: "",
orientationTypeItemTypes: [{ "value": 0, "name": "竖屏" }, { "value": 1, "name": "横屏" }, { "value": 2, "name": "自动" }] as ItemType[],
sourceTypeItemTypes: [{ "value": 0, "name": "从相册中选择视频" }, { "value": 1, "name": "拍摄视频" }, { "value": 2, "name": "从相册中选择视频或拍摄视频" }] as ItemType[],
sourceTypeItems: [["album"], ["camera"], ["album", "camera"]] as Source[][],
cameraItemTypes: [{ "value": 0, "name": "后置摄像头" }, { "value": 1, "name": "前置摄像头" }] as ItemType[],
albumModeTypes: [{ "value": 0, "name": "自定义视频选择器" }, { "value": 1, "name": "系统视频选择器" }] as ItemType[],
albumModeTypeItems: ["custom", "system"],
cameraItems: ["back", "front"] as Camera[],
sourceType: ["album", "camera"] as Source[],
orientationType: "portrait",
orientationTypeItems: ["portrait", "landscape", "auto"],
compressed: true,
maxDuration: 60,
camera: "back" as Camera,
videoInfo: "",
videoCoverImage: "",
albumMode: "custom"
}
},
onHide() {
console.log("Page Hide");
},
methods: {
chooseVideo() {
uni.chooseVideo({
sourceType: this.sourceType,
// #ifdef APP
compressed: this.compressed,
pageOrientation: this.orientationType,
// #endif
maxDuration: this.maxDuration,
// #ifdef APP-ANDROID
albumMode: this.albumMode,
// #endif
camera: this.camera,
success: (res) => {
console.log("chooseVideo success", JSON.stringify(res));
this.src = res.tempFilePath;
this.videoInfo = `视频长度: ${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) {
this.videoCoverImage = _res.thumbTempFilePath!
}
}
});
// #endif
},
fail: (err) => {
uni.showModal({
title: "选择视频失败",
content: JSON.stringify(err),
showCancel: false
});
}
});
},
onOrientationTypeChange(value : number) {
this.orientationType = this.orientationTypeItems[value];
},
onSourceTypeChange(value : number) {
this.sourceType = this.sourceTypeItems[value];
},
onCompressedChange(value : boolean) {
this.compressed = value;
},
onMaxDurationConfirm(value : number) {
this.maxDuration = value;
},
onCameraChange(value : number) {
this.camera = this.cameraItems[value];
},
onAlbumModeChange(value : number) {
this.albumMode = this.albumModeTypeItems[value]
}
}
}
</script>
<style>
.video {
align-self: center;
width: 300px;
height: 225px;
}
</style>
名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
---|---|---|---|---|---|
errMsg | string | 是 | - | 错误信息 |
albumMode
为system
时,可以正常上架google play。同时需要注意在manifest.json中将<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
和<uses-permission android:name="android.permission.READ_MEDIA_VIDEO" />
权限移除。配置方式参考移除Android权限.maxDuration
和camera
属性在部分手机上不生效。sourceType
为['album']
、albumMode
为system
、compressed
为true
时,支持返回Uri地址。sizeType
仅支持设置['original']
或['compressed']
。在Android 11及以上的系统中,设置system
调用的是系统的视频选择器,低于android 11的系统中会调用系统的文件选择器。