8.2.7 视频组件控制
wx.createVideoContext(videoId)
接口用于创建并返回 video 上下文 videoContext 对象。videoContext 通过 videoId 跟一个 video 组件绑定,通过它可以操作一个 video 组件。 videoContext对象的方法,如表8-22所示:
表8-22
方法 | 参数 | 说明 |
---|---|---|
play | 无 | 播放 |
pause | 无 | 暂停 |
seek | position | 跳转到指定位置,单位 s |
sendDanmu | danmu | 发送弹幕,danmu 包含两个属性 text, color。 |
示例代码:
wxml中:
<view class="section tc">
<video id="myVideo" src="https://xxxx.xxxx.com/xxxx" enable-danmu danmu-btn controls></video>
<view class="btn-area">
<input bindblur="bindInputBlur"/>
<button bindtap="bindSendDanmu">发送弹幕</button>
</view>
</view>
js中:
function getRandomColor () {
let 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: function (res) {
this.videoContext = wx.createVideoContext('myVideo')
},
inputValue: '',
bindInputBlur: function(e) {
this.inputValue = e.detail.value
},
bindSendDanmu: function () {
this.videoContext.sendDanmu({
text: this.inputValue,
color: getRandomColor()
})
}
})