1.选择视频
使用wx.chooseVideo(OBJECT)拍摄视频或从手机相册中选视频,返回视频的临时文件路径,参数说明如下:
succcess()返回参数说明:
ps:wx.chooseVideo()获得的视频仅能在小程序启动期间临时使用,如需持久保存,需要主动调用wx.saveFile()进行保存,下次启动时才能访问得到。
2.保存视频
wx.saveVideoToPhotosAlbum(OBJECT)保存视频到系统相册,需用户授权scope.writePhotosAlbum,参数说明如下:
3.视频组件控制
使用wx.createVideoContext(video,this)创建并返回视频上下文对象videoContext,其通过videoId和一个video组件绑定,通过它可以操作一个video组件。在自定义组件下,第二个参数传入组件实例this,以操作组件内的<video>组件。其方法说明如下:
综合应用示例:
<!--wxml-->
<view class="title">第6章 媒体API</view>
<view class="demo-box">
<view class="title">视频管理综合应用</view>
<button bindtap="chooseVideo">选择视频</button>
<video id="myVideo" src="{{src}}" enable-danmu danmu-btn controls></video>
<button type="primary" size="mini" bindtap="play">播放</button>
<button type="primary" size="mini" bindtap="pause">暂停</button>
<button type="primary" size="mini" bindtap="saveVideo">保存视频</button>
<input placeholder="请在此处填写弹幕内容" bindblur="bindInputBlur"></input>
<button type="primary" size="mini" bindtap="bindSendDanmu">发送弹幕</button>
</view>
/*wxss*/
input{
border: 1rpx solid lightblue;
height: 90rpx;
margin: 10rpx;
}
button{
margin-left: 50rpx;
}
//js
function getRandomColor(){
let rgb=[]
for(let i=0;i<3;i++){
let color=Math.floor(Math.random()*256).toString(16)
console.log(color)
color=color.length==1?'0'+color:color
rgb.push(color)
}
console.log("#" + rgb.join(''))
return "#" + rgb.join('')
}
Page({
//选择视频
chooseVideo:function(){
var that = this
wx.chooseVideo({
sourceType: ['album','camera'],
maxDuration: 60,
camera:'back',
success: function(res) {
that.setData({
src:res.tempFilePath
})
}
})
},
//开始播放
play:function(){
this.videoContext.play()
},
//暂停播放
pause:function(){
this.videoContext.pause()
},
//保存视频
saveVideo:function(){
var src = this.data.src
wx.saveVideoToPhotosAlbum({
filePath: src,
success: function(res) {
wx.showToast({
title: '保存成功!',
})
}
})
},
inputValue:'', //弹幕文本内容
//更新弹幕文本内容
bindInputBlur:function(e){
this.inputValue=e.detail.value
},
//发送弹幕
bindSendDanmu:function(){
this.videoContext.sendDanmu({
text:this.inputValue,
color:getRandomColor()
})
},
onLoad:function(options){
this.videoContext = wx.createVideoContext("myVideo")
}
})