tencent cloud

视频

PDF
聚焦模式
字号
最后更新时间: 2026-03-20 18:44:06

saveVideoToPhotosAlbum

该 API 使用方法为 wx.saveVideoToPhotosAlbum(Object object)
功能说明:保存视频到系统相册。支持 mp4视频格式。
参数及说明:Object object。
属性
类型
默认值
必填
说明
filePath
string
-
视频文件路径,可以是临时文件路径也可以是永久文件路径
success
function
-
接口调用成功的回调函数
fail
function
-
接口调用失败的回调函数
complete
function
-
接口调用结束的回调函数(调用成功、失败都会执行)
示例代码
wx.saveVideoToPhotosAlbum({
filePath: 'wxfile://xxx',
success(res) {
console.log(res.errMsg)
},
})

createVideoContext

该 API 使用方法为 VideoContext wx.createVideoContext(string id, Object this)
功能说明:创建 video 上下文 VideoContext 对象。
参数及说明:
string id:<video> 组件的 id。
在自定义组件下,当前组件实例的 this,以操作组件内 <video> 组件。
返回值:VideoContext

chooseVideo

该 API 使用方法为 wx.chooseVideo(Object object)
功能说明:拍摄视频或从手机相册中选视频。
参数及说明:Object object。
属性
类型
默认值
必填
说明
sourceType
Array.<string>
['album', 'camera']
视频选择来源
compressed
boolean
true
是否压缩所选择的视频文件
maxDuration
number
60
拍摄视频,最长拍摄时间,单位:秒
camera
string
'back'
默认拉起的是前置或者后置摄像头。部分 Android 手机下由于系统 ROM 不支持无法生效
success
function
-
接口调用成功的回调函数
fail
function
-
接口调用失败的回调函数
complete
function
-
接口调用结束的回调函数(调用成功、失败都会执行)
object.sourceType 的合法值
说明
album
从相册选择视频
camera
使用相机拍摄视频
object.camera 的合法值
说明
back
默认拉起后置摄像头
front
默认拉起前置摄像头
object.success 回调函数参数:Object res。
属性
类型
说明
tempFilePath
string
选定视频的临时文件路径
duration
number
选定视频的时间长度
size
number
选定视频的数据量大小,单位 B
height
number
返回选定视频的高度
width
number
返回选定视频的宽度
示例代码
wx.chooseVideo({
sourceType: ['album', 'camera'],
maxDuration: 60,
camera: 'back',
success(res) {
console.log(res.tempFilePath)
},
})

compressVideo

该 API 使用方法为 wx.compressVideo(Object object)
功能说明:压缩视频接口。开发者可指定压缩质量 quality 进行压缩。当需要更精细的控制时,可指定 bitratefps和 resolution,当 quality 传入时,这三个参数将被忽略。
参数及说明:Object object。
属性
类型
必填
说明
src
string
视频文件路径,可以是临时文件路径也可以是永久文件路径
quality
string
压缩质量,合法值为:
low:低
medium:中
high:高
bitrate
number
码率,单位 kbps
fps
number
帧率
resolution
number
相对于原视频的分辨率比例,取值范围(0, 1]
success
function
接口调用成功的回调函数
fail
function
接口调用失败的回调函数
complete
function
接口调用结束的回调函数(调用成功、失败都会执行)
object.success 回调函数参数:Object res
属性
类型
说明
tempFilePath
string
压缩后的临时文件地址
size
string
压缩后的大小,单位 KB

chooseMedia

该 方法 使用方式为 wx.chooseMedia(Object object)
功能说明:拍摄或从手机相册中选择图片或视频。
参数及说明:Object object
属性
类型
默认值
必填
说明
count
number
15
最多可以选择的文件个数,最多可支持15个文件
mediaType
Array.<string>
['image','video']
文件类型,合法值为:
image:只能拍摄图片或从相册选择图片
video:只能拍摄或从相册选择视频
注意:
iOS 可选择任何系统照片 App 支持的图片格式。通常包括 JPG, PNG, HEIC (取决于设备型号和 iOS 版本), 以及 GIF 等。
sourceType
Array.<string>
['album', 'camera']
图片和视频选择的来源,合法值为:
album:从相册选择
camera:使用相机拍摄
maxDuration
number
10
拍摄视频最长拍摄时间,单位秒。时间范围为 3s 至 60s。不限制相册。
sizeType
Array.<string>
['original', 'compressed']
仅对 mediaType 为 image 时有效,是否压缩所选文件
camera
string
'back'
仅在 sourceType 为 camera 时生效,使用前置或后置摄像头,合法值为:
back:使用后置摄像头
front:使用前置摄像头
success
function
-
接口调用成功的回调函数
fail
function
-
接口调用失败的回调函数
complete
function
-
接口调用结束的回调函数(调用成功、失败都会执行)
object.success 回调函数参数:Object res
属性
类型
说明
tempFiles
Array.<Object>
本地临时文件列表
type
string
文件类型,有效值有 image 、video
tempFiles 结构属性
结构属性
类型
说明
tempFilePath
string
本地临时文件路径(本地目录)
size
number
本地临时文件大小,单位 B
duration
number
视频的时间长度
height
number
视频的高度
width
number
视频的宽度
thumbTempFilePath
string
视频缩略图临时文件路径
fileType
string
文件类型,合法值有:
image:图片
video:视频
示例代码
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)
}
})

VideoContext

功能说明:VideoContext 实例,可通过 wx.createVideoContext 获取。videoContext 通过 id 跟一个 <video> 组件绑定,操作对应的<video> 组件。
示例代码
对应的 WXML 文件:
<view class="section tc">
<video
id="myVideo"
src="http://wxsnsdy.tc.qq.com/105/20210/snsdyvideodownload?filekey=30280201010421301f0201690402534804102ca905ce620b1241b726bc41dcff44e00204012882540400&bizid=1023&hy=SH&fileparam=302c020101042530230204136ffd93020457e3c4ff02024ef202031e8d7f02030f42400204045a320a0201000400"
enable-danmu
danmu-btn
controls
></video>
<view class="btn-area">
<input bindblur="bindInputBlur" />
<button bindtap="bindSendDanmu">发送弹幕</button>
</view>
</view>
对应的 JS 文件:
function getRandomColor() {
const rgb = []
for (let i = 0; i < 3; ++i) {
let color = Math.floor(Math.random() * 256).toString(16)
color = color.length == 1 ? '0' + color : color
rgb.push(color)
}
return '#' + rgb.join('')
}

Page({
onReady(res) {
this.videoContext = wx.createVideoContext('myVideo')
},
inputValue: '',
bindInputBlur(e) {
this.inputValue = e.detail.value
},
bindSendDanmu() {
this.videoContext.sendDanmu({
text: this.inputValue,
color: getRandomColor(),
})
},
})

方法集



帮助和支持

本页内容是否解决了您的问题?

填写满意度调查问卷,共创更好文档体验。

文档反馈