视频

最近更新时间:2023-12-06 15:14:27

我的收藏

saveVideoToPhotosAlbum

该 API 使用方法为 wx.saveVideoToPhotosAlbum(Object object)
功能说明:保存视频系统相册。
参数及说明:Object object。
属性
类型
默认值
必填
说明
filePath
string
-
视频文件路径,可以是临时文件路径也可以是永久文件路径
success
function
-
接口调用成功的回调函数
fail
function
-
接口调用失败的回调函数
complete
function
-
接口调用结束的回调函数(调用成功、失败都会执行)
示例代码
wx.saveVideoToPhotosAlbum({
filePath: 'qqfile://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.\\
['album', 'camera']
视频选择来源
compressed
boolean
ture
是否压缩所选择的视频文件
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
选定视频的数据量大小
height
number
返回选定视频的高度
width
number
返回选定视频的宽度
thumbTempFilePath
string
视频缩略图临时文件路径
示例代码
wx.chooseVideo({
sourceType: ['album', 'camera'],
maxDuration: 60,
camera: 'back',
success(res) {
console.log(res.tempFilePath)
},
})

compressVideo

该 API 使用方法为 wx.compressVideo(Object object)
功能说明:压缩视频接口。开发者可指定压缩质量 quality 进行压缩。当需要更精细的控制时,可指定 bitrate、fps、和 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
9
最多可以选择的文件个数,基础库2.25.0前,最多可支持9个文件,2.25.0及以后最多可支持20个文件
mediaType
Array.<string>
['image','video']
文件类型,合法值为:
image:只能拍摄图片或从相册选择图片
video:只能拍摄或从相册选择视频
mix:可同事选择图片和视频
sourceType
Array.<string>
['album', 'camera']
图片和视频选择的来源,合法值为:
album:从相册选择
camera:使用相机拍摄
maxDuratuib
number
10
是否压缩所选文件,基础库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>
本地临时文件列表
type
string
文件类型,有效值有 image 、video、mix
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="https://qzonestyle.gtimg.cn/qzone/qzact/act/external/qq-video/qq-video.mp4"
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(),
})
},
})

方法集