微信小程序視頻彈幕位置隨機(jī)
本文實(shí)例為大家分享了微信小程序視頻彈幕位置隨機(jī)的具體代碼,供大家參考,具體內(nèi)容如下
最近更新開(kāi)發(fā)工具之后,微信小程序視頻播放彈幕不再自動(dòng)隨機(jī),所以就用了一個(gè)比較取巧的方法(多條空彈幕和自己要發(fā)送的彈幕一起發(fā)送,利用隨機(jī)數(shù)控制順序就行了);
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 style="margin: 30rpx auto" class="weui-label">彈幕內(nèi)容</view>
<input bindblur="bindInputBlur" class="weui-input" type="text" placeholder="在此處輸入彈幕內(nèi)容" />
<button style="margin: 30rpx auto" bindtap="bindSendDanmu" class="page-body-button" type="primary" formType="submit">發(fā)送彈幕</button>
<navigator style="margin: 30rpx auto" 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)
}
})
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
layui 數(shù)據(jù)表格 根據(jù)值(1=業(yè)務(wù),2=機(jī)構(gòu))顯示中文名稱(chēng)示例
今天小編就為大家分享一篇layui 數(shù)據(jù)表格 根據(jù)值(1=業(yè)務(wù),2=機(jī)構(gòu))顯示中文名稱(chēng)示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10
js如何判斷訪問(wèn)是來(lái)自搜索引擎(蜘蛛人)還是直接訪問(wèn)
這篇文章主要介紹了js如何判斷訪問(wèn)是來(lái)自搜索引擎(蜘蛛人)還是直接訪問(wèn),需要的朋友可以參考下2015-09-09
JavaScript中l(wèi)ayim之整合右鍵菜單的示例代碼
這篇文章主要介紹了JavaScript中l(wèi)ayim之整合右鍵菜單的示例代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02
限時(shí)搶購(gòu)-倒計(jì)時(shí)的完整實(shí)例(分享)
下面小編就為大家?guī)?lái)一篇限時(shí)搶購(gòu)-倒計(jì)時(shí)的完整實(shí)例(分享)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09
JavaScript設(shè)計(jì)模式經(jīng)典之工廠模式
工廠模式定義一個(gè)用于創(chuàng)建對(duì)象的接口,這個(gè)接口由子類(lèi)決定實(shí)例化哪一個(gè)類(lèi)。接下來(lái)通過(guò)本文給大家介紹JavaScript設(shè)計(jì)模式經(jīng)典之工廠模式,感興趣的朋友一起學(xué)習(xí)吧2016-02-02
javascript下string.format函數(shù)補(bǔ)充
在上一篇中,自謙懶人的咚鏘留言指出樓豬改寫(xiě)的format函數(shù)在參數(shù)輸入11個(gè)后不起作用了2010-08-08
JS判斷網(wǎng)頁(yè)廣告是否被瀏覽器攔截過(guò)濾的代碼
這篇文章主要介紹了JS判斷網(wǎng)頁(yè)廣告是否被瀏覽器攔截過(guò)濾的代碼,需要的朋友可以參考下2015-04-04

