微信小程序之----video視頻播放

vidaodom

我如今看到的官方文檔是不帶danmu(彈幕)屬性的,以前是有的,不過如今這個屬性還能夠生效。控制視頻的狀態能夠根據video標籤的惟一id獲得一個對象實例。video組件並不具有action屬性,不能經過action來控制。ide

  • .wxml
<view class="section tc">
  <video src="{{src}}"   controls ></video>
  <view class="btn-area">
    <button bindtap="bindButtonTap">獲取視頻</button>
  </view>
</view>

<!-- 
  danmu-list:彈幕列表
  enable-danmu:是否顯示彈幕
  danmu-btn:彈幕按鈕
  controls:是否顯示視頻控件,並無什麼用
 -->
<view class="section tc">
  <video id="myVideo" src="http://wxsnsdy.tc.qq.com/105/20210/snsdyvideodownload?filekey=30280201010421301f0201690402534804102ca905ce620b1241b726bc41dcff44e00204012882540400&bizid=1023&hy=SH&fileparam=302c020101042530230204136ffd93020457e3c4ff02024ef202031e8d7f02030f42400204045a320a0201000400" binderror="videoErrorCallback" danmu-list="{{danmuList}}" enable-danmu danmu-btn controls></video>
  <view class="btn-area">
    <button bindtap="bindButtonTap">獲取視頻</button>
    <input bindblur="bindInputBlur"/>
    <button bindtap="bindSendDanmu">發送彈幕</button>
  </view>
</view>

<!-- 如今的video就下面三個屬性 -->
<video src="" binderror="" hidden></video>
  • .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: '',
    data: {
        src: '',
    danmuList: [
      {
        text: '第 1s 出現的彈幕',
        color: '#ff0000',
        time: 1
      },
      {
        text: '第 3s 出現的彈幕',
        color: '#ff00ff',
        time: 3
      }
    ]
    },
  bindInputBlur: function(e) {
    this.inputValue = e.detail.value
  },
    bindButtonTap: function() {  //視頻下載
        var that = this
        wx.chooseVideo({
            sourceType: ['album', 'camera'],
            maxDuration: 60,
            camera: ['front','back'],
            success: function(res) {
                that.setData({
                    src: res.tempFilePath
                })
            }
        })
    },
  bindSendDanmu: function () {
    this.videoContext.sendDanmu({
      text: this.inputValue,
      color: getRandomColor()
    })
  },
    videoErrorCallback: function(e) {
      console.log('視頻錯誤信息:');
      console.log(e.detail.errMsg);
    }
})
  • 效果

相關文章
相關標籤/搜索