溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么在微信小程序中實(shí)現(xiàn)一個(gè)視頻彈幕效果

發(fā)布時(shí)間:2021-04-30 15:00:21 來源:億速云 閱讀:331 作者:Leah 欄目:開發(fā)技術(shù)

本篇文章為大家展示了怎么在微信小程序中實(shí)現(xiàn)一個(gè)視頻彈幕效果,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。

wxml代碼

<!--pages/study/video/videoplay/videoplay.wxml-->
<view class="page-body">
  <view class="page-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 
      show-center-play-btn='{{false}}' 
      show-play-btn="{{true}}" 
      controls
      picture-in-picture-mode="{{['push', 'pop']}}"
      bindenterpictureinpicture='bindVideoEnterPictureInPicture'
      bindleavepictureinpicture='bindVideoLeavePictureInPicture'
    ></video>
    <view  class="weui-label">彈幕內(nèi)容</view>
    <input bindblur="bindInputBlur" class="weui-input" type="text" placeholder="在此處輸入彈幕內(nèi)容" />
    <button   bindtap="bindSendDanmu" class="page-body-button" type="primary" formType="submit">發(fā)送彈幕</button>
    <navigator   url="picture-in-picture" hover-class="other-navigator-hover">
      <button type="primary" class="page-body-button" bindtap="bindPlayVideo">小窗模式</button>
    </navigator>
  </view>
</view>

js代碼

// pages/study/video/videoplay/videoplay.js
var that;
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({
  onShareAppMessage() {
    return {
      title: 'video',
      path: 'page/component/pages/video/video'
    }
  },
 
  onReady() {
    that = this;
    this.videoContext = wx.createVideoContext('myVideo')
  },
 
  onHide() {
 
  },
 
  inputValue: '',
  data: {
    src: '',
    danmuList:
    [{
      text: '第 1s 出現(xiàn)的彈幕',
      color: '#ff0000',
      time: 1
    }, {
      text: '第 3s 出現(xiàn)的彈幕',
      color: '#ff00ff',
      time: 3
    }],
  },
 
  bindInputBlur(e) {
    this.inputValue = e.detail.value
  },
 
  bindButtonTap() {
    const that = this
    wx.chooseVideo({
      sourceType: ['album', 'camera'],
      maxDuration: 60,
      camera: ['front', 'back'],
      success(res) {
        that.setData({
          src: res.tempFilePath
        })
      }
    })
  },
 
  bindVideoEnterPictureInPicture() {
    console.log('進(jìn)入小窗模式')
  },
 
  bindVideoLeavePictureInPicture() {
    console.log('退出小窗模式')
  },
 
  bindPlayVideo() {
    this.videoContext.play()
  },
  bindSendDanmu() {
    // 利用循環(huán)和隨機(jī)數(shù)調(diào)整位置
    var ranNum = Math.floor(Math.random()*10);
    var danmuList = [];
    for (let index = 0; index < 10; index++) {
      danmuList.push('');
    }
    danmuList[ranNum] = this.inputValue;
    for (let index = 0; index < danmuList.length; index++) {
      this.videoContext.sendDanmu({
        text: danmuList[index],
        color: '#ff0000'
      });
    }
  },
 
  videoErrorCallback(e) {
    console.log('視頻錯(cuò)誤信息:')
    console.log(e.detail.errMsg)
  }
})

上述內(nèi)容就是怎么在微信小程序中實(shí)現(xiàn)一個(gè)視頻彈幕效果,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI