微信小程序視頻彈幕發(fā)送功能的實(shí)現(xiàn)
1. 目錄結(jié)構(gòu)
2. videoDanmu
<!-- 視頻區(qū)域 --> <video src="{{src}}" id='video' danmu-btn enable-danmu danmu-list="{{ danmuList }}" > </video> <!-- 彈幕發(fā)送區(qū)域 --> <view class='sendDanmu'> <textarea name="" id="" cols="30" rows="10" placeholder="請輸入彈幕" bindinput="handleDanmuContent" ></textarea> <button type='primary' bindtap="handleSendDanmu"> 發(fā)送 </button> </view> <!-- 是否隨機(jī)顏色 --> <view class='selectColor'> <text> 隨機(jī)顏色 </text> <switch bindchange='handleChange'></switch> </view> <!-- 選擇默認(rèn)顏色 --> <view class='selectDefaultColor' bindtap="handleGotoSelectColor"> <text> 選擇顏色 </text> <view style="background: {{ defaultColor }}"></view> </view>
/* pages/videoDanmu/videoDanmu.wxss */ #video{ width: 100%; height: calc(750rpx * 225 / 300); } .sendDanmu{ display: flex; height: 100rpx; } .sendDanmu textarea{ border: 1px solid #ddd; height: 100rpx; } .selectColor{ display: flex; justify-content: space-between; margin-top: 20rpx; border: 1px solid #ddd; align-items: center; } .selectDefaultColor{ display: flex; justify-content: space-between; align-items: center; border: 1px solid #ddd; margin-top: 20rpx; margin-right: 30rpx; } .selectDefaultColor view{ width: 50rpx; height: 50rpx; background-color: red; }
// pages/videoDanmu/videoDanmu.js Page({ /** * 頁面的初始數(shù)據(jù) */ data: { // 視頻地址 src:"http://wxsnsdy.tc.qq.com/105/20210/snsdyvideodownload?filekey=30280201010421301f0201690402534804102ca905ce620b1241b726bc41dcff44e00204012882540400&bizid=1023&hy=SH&fileparam=302c020101042530230204136ffd93020457e3c4ff02024ef202031e8d7f02030f42400204045a320a0201000400", danmuList: [{ text: '第 1s 出現(xiàn)的彈幕', color: '#ff0000', time: 1 }, { text: '第 3s 出現(xiàn)的彈幕', color: '#ff00ff', time: 3 }], // 初始彈幕列表 defaultColor: 'red', // 彈幕默認(rèn)顏色 isRandomColor: false, // 控制彈幕是否隨機(jī)顏色 }, // 獲取彈幕內(nèi)容 handleDanmuContent(e){ this.content = e.detail.value; }, // 隨機(jī)彈幕顏色 randomColor(){ let red = Math.random() * 255; let green = Math.random() * 255; let blue = Math.random() * 255; return `rgb(${red}, ${green}, ${blue})` }, // 發(fā)送彈幕 handleSendDanmu(){ let color = ''; // 通過狀態(tài)值判斷是默認(rèn)顏色還是隨機(jī)顏色 if(this.data.isRandomColor){ color = this.randomColor() }else{ color = this.data.defaultColor } this.videoContext.sendDanmu({ text: this.content, color }) }, // 處理彈幕是否隨機(jī)顏色 handleChange(e){ // console.log(e.detail.value) this.setData({ isRandomColor: e.detail.value }) }, // 跳轉(zhuǎn)到選擇顏色頁面 handleGotoSelectColor(){ wx.navigateTo({ url: "/pages/selectColor/selectColor", }) }, /** * 生命周期函數(shù)--監(jiān)聽頁面初次渲染完成 */ onReady: function () { // 創(chuàng)建 video 上下文 VideoContext 對象。通過這個(gè)對象發(fā)送彈幕 this.videoContext = wx.createVideoContext('video', this) } })
3. selectColor
<view class='color-wrap'> <view wx:for="{{ color }}" wx:key='index' style="background: {{ item.number }}" bindtap="handleGetColor" data-color=" {{ item.number }} " > {{ item.color }} </view> </view>
/* pages/selectColor/selectColor.wxss */ .color-wrap{ display: flex; justify-content: space-between; flex-wrap: wrap; } .color-wrap view{ width: 100rpx; height: 100rpx; background-color: red; margin-top: 20rpx; }
// pages/selectColor/selectColor.js import color from './color' Page({ /** * 頁面的初始數(shù)據(jù) */ data: { color }, // 獲取選擇的顏色 handleGetColor(e){ // console.log(e.currentTarget.dataset.color) const color = e.currentTarget.dataset.color; // 獲取頁面棧 const pages = getCurrentPages() const page = pages[0]; // 上一頁面 page.setData({ defaultColor: color }) // 返回上一頁面 wx.navigateBack({ delta: 0, }) } })
export default [ { key: 1, color: ' 白色 ', number: '#FFFFFF' }, { key: 2, color: ' 紅色 ', number: '#FF0000' }, { key: 3, color: ' 綠色 ', number: '#00FF00' }, { key: 4, color: ' 藍(lán)色 ', number: '#0000FF' }, { key: 5, color: ' 牡丹紅 ', number: '#FF00FF' }, { key: 6, color: ' 青色 ', number: '#00FFFF' }, { key: 7, color: ' 黃色 ', number: '#FFFF00' }, { key: 8, color: ' 黑色 ', number: '#000000' }, { key: 9, color: ' 海藍(lán) ', number: '#70DB93' }, { key: 10, color: ' 巧克力色 ', number: '#5C3317' }, { key: 11, color: ' 藍(lán)紫色 ', number: '#9F5F9F' }, { key: 12, color: ' 黃銅色 ', number: '#B5A642' }, { key: 13, color: ' 亮金色 ', number: '#D9D919' }, { key: 14, color: ' 棕色 ', number: '#A67D3D' }, { key: 15, color: ' 青銅色 ', number: '#8C7853' }, { key: 16, color: ' 2號青銅色 ', number: '#A67D3D' }, { key: 17, color: ' 士官服藍(lán)色 ', number: '#5F9F9F' }, { key: 18, color: ' 冷銅色 ', number: '#D98719' }, { key: 19, color: ' 銅色 ', number: '#B87333' }, { key: 20, color: ' 珊瑚紅 ', number: '#FF7F00' }, { key: 21, color: ' 紫藍(lán)色 ', number: '#42426F' }, { key: 22, color: ' 深棕 ', number: '#5C4033' }, { key: 23, color: ' 深綠 ', number: '#2F4F2F' }, { key: 24, color: ' 深銅綠色 ', number: '#4A766E' }, { key: 25, color: ' 深橄欖綠 ', number: '#4F4F2F' }, { key: 26, color: ' 深蘭花色 ', number: '#9932CD' }, { key: 27, color: ' 深紫色 ', number: '#871F78' }, { key: 28, color: ' 深石板藍(lán) ', number: '#6B238E' }, { key: 29, color: ' 深鉛灰色 ', number: '#2F4F4F' }, { key: 30, color: ' 深棕褐色 ', number: '#97694F' }, { key: 32, color: ' 深綠松石色 ', number: '#7093DB' }, { key: 33, color: ' 暗木色 ', number: '#855E42' }, { key: 34, color: ' 淡灰色 ', number: '#545454' }, { key: 35, color: ' 土灰玫瑰紅色 ', number: '#856363' }, { key: 36, color: ' 長石色 ', number: '#D19275' }, { key: 37, color: ' 火磚色 ', number: '#8E2323' }, { key: 38, color: ' 森林綠 ', number: '#238E23' }, { key: 39, color: ' 金色 ', number: '#CD7F32' }, { key: 40, color: ' 鮮黃色 ', number: '#DBDB70' }, { key: 41, color: ' 灰色 ', number: '#C0C0C0' }, { key: 42, color: ' 銅綠色 ', number: '#527F76' }, { key: 43, color: ' 青黃色 ', number: '#93DB70' }, { key: 44, color: ' 獵人綠 ', number: '#215E21' }, { key: 45, color: ' 印度紅 ', number: '#4E2F2F' }, { key: 46, color: ' 土黃色 ', number: '#9F9F5F' }, { key: 47, color: ' 淺藍(lán)色 ', number: '#C0D9D9' }, { key: 48, color: ' 淺灰色 ', number: '#A8A8A8' }, { key: 49, color: ' 淺鋼藍(lán)色 ', number: '#8F8FBD' }, { key: 59, color: ' 淺木色 ', number: '#E9C2A6' }, { key: 60, color: ' 石灰綠色 ', number: '#32CD32' }, { key: 61, color: ' 桔黃色 ', number: '#E47833' }, { key: 62, color: ' 褐紅色 ', number: '#8E236B' }, { key: 63, color: ' 中海藍(lán)色 ', number: '#32CD99' }, { key: 64, color: ' 中藍(lán)色 ', number: '#3232CD' }, { key: 65, color: ' 中森林綠 ', number: '#6B8E23' }, { key: 66, color: ' 中鮮黃色 ', number: '#EAEAAE' }, { key: 67, color: ' 中蘭花色 ', number: '#9370DB' }, { key: 68, color: ' 中海綠色 ', number: '#426F42' }, { key: 69, color: ' 中石板藍(lán)色 ', number: '#7F00FF' }, { key: 70, color: ' 中春綠色 ', number: '#7FFF00' }, { key: 71, color: ' 中綠松石色 ', number: '#70DBDB' }, { key: 72, color: ' 中紫紅色 ', number: '#DB7093' }, { key: 73, color: ' 中木色 ', number: '#A68064' }, { key: 74, color: ' 深藏青色 ', number: '#2F2F4F' }, { key: 75, color: ' 海軍藍(lán) ', number: '#23238E' }, { key: 76, color: ' 霓虹籃 ', number: '#4D4DFF' }, { key: 77, color: ' 霓虹粉紅 ', number: '#FF6EC7' }, { key: 78, color: ' 新深藏青色 ', number: '#00009C' }, { key: 79, color: ' 新棕褐色 ', number: '#EBC79E' }, { key: 80, color: ' 暗金黃色 ', number: '#CFB53B' }, { key: 81, color: ' 橙色 ', number: '#FF7F00' }, ];
4. 效果圖
顏色選擇
到此這篇關(guān)于微信小程序視頻彈幕發(fā)送功能的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)小程序視頻彈幕發(fā)送內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
javascript中的Base64、UTF8編碼與解碼詳解
本文給大家介紹的是javascript中的Base64、UTF8編碼與解碼的函數(shù)源碼分享以及使用范例,十分實(shí)用,推薦給小伙伴們,希望大家能夠喜歡。2015-03-03JavaScript Promise原理與實(shí)現(xiàn)刨析
首先呢,Promise是異步中比較重要的知識點(diǎn),學(xué)習(xí)的最好方法就是掌握它的基本原理。所以這一篇主要說一下如何用JS來實(shí)現(xiàn)一個(gè)自己的promise2022-10-10JavaScript錯(cuò)誤處理操作實(shí)例詳解
這篇文章主要介紹了JavaScript錯(cuò)誤處理操作,結(jié)合實(shí)例形式分析了javascript常見的錯(cuò)誤類型、錯(cuò)誤處理語句以及相關(guān)使用技巧,需要的朋友可以參考下2019-01-01js 完美圖片新聞輪轉(zhuǎn)效果,騰訊大粵網(wǎng)首頁圖片輪轉(zhuǎn)改造而來
找過很多圖片輪詢效果,和我的要求總是有些出入,不能十全十美。不是功能不全,就是太多花哨。本想自己開發(fā)一個(gè),鑒于瑣事拖延,遲遲未能開始2011-11-11JavaScript實(shí)現(xiàn)打字效果的方法
這篇文章主要介紹了JavaScript實(shí)現(xiàn)打字效果的方法,可實(shí)現(xiàn)文字陸續(xù)出現(xiàn)的打字效果,涉及javascript時(shí)間函數(shù)及頁面元素獲取的相關(guān)技巧,需要的朋友可以參考下2015-07-07JavaScript實(shí)現(xiàn)二級菜單的制作
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)二級菜單的制作,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10JavaScript創(chuàng)建數(shù)組的方法詳解
這篇文章主要為大家介紹了JavaScript創(chuàng)建數(shù)組的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2021-12-12利用JavaScript差集實(shí)現(xiàn)一個(gè)對比小工具
這篇文章主要給大家介紹了關(guān)于利用JavaScript差集實(shí)現(xiàn)一個(gè)對比小工具的相關(guān)資料,雖然實(shí)現(xiàn)的界面不是太好看,但好在功能實(shí)用即可,需要的朋友可以參考下2021-07-07