VideoContext

VideoContext 实例,可通过 wx.createVideoContext 获取。

VideoContext 通过 id 跟一个 video 组件绑定,操作对应的 video 组件。

方法

VideoContext.play()

播放视频

VideoContext.pause()

暂停视频

VideoContext.stop()

停止视频

VideoContext.seek(number position)

跳转到指定位置

VideoContext.sendDanmu(Object data)

发送弹幕

VideoContext.playbackRate(number rate)

设置倍速播放

VideoContext.requestFullScreen(Object object)

进入全屏

VideoContext.exitFullScreen()

退出全屏

VideoContext.showStatusBar()

显示状态栏,仅在iOS全屏下有效

VideoContext.hideStatusBar()

隐藏状态栏,仅在iOS全屏下有效

示例代码

在开发者工具中预览效果

  1. <view class="section tc">
  2. <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>
  3. <view class="btn-area">
  4. <input bindblur="bindInputBlur"/>
  5. <button bindtap="bindSendDanmu">发送弹幕</button>
  6. </view>
  7. </view>
  1. function getRandomColor () {
  2. let rgb = []
  3. for (let i = 0 ; i < 3; ++i) {
  4. let color = Math.floor(Math.random() * 256).toString(16)
  5. color = color.length == 1 ? '0' + color : color
  6. rgb.push(color)
  7. }
  8. return '#' + rgb.join('')
  9. }
  10. Page({
  11. onReady (res) {
  12. this.videoContext = wx.createVideoContext('myVideo')
  13. },
  14. inputValue: '',
  15. bindInputBlur (e) {
  16. this.inputValue = e.detail.value
  17. },
  18. bindSendDanmu () {
  19. this.videoContext.sendDanmu({
  20. text: this.inputValue,
  21. color: getRandomColor()
  22. })
  23. }
  24. })