微信小程序video、form组件实现发送弹幕

微信小程序实现弹幕内容获取:
js方法:

//js
Page({
    data:{
      inputValue: "",
    },
    
    bindInputBlur(e) {
      this.inputValue =  e.detail.value
      // console.log(this.inputValue)
      this.setData({
        inputValue : e.detail.value
      })
    },
     })

wxml页面样式:

<!-- xml -->
<view class="btn-area">
  <view class="page-section">
    <view class="cells__title">实时获取输入值:{{inputValue}}</view>
    <view class="cells_after-title">
      <view class="cell_input">
        <input  bindblur="bindInputBlur" placeholder="这是一个输入框" />
      </view>
    </view>
  </view>
 /view>

对应的wxss文件:

/** wxss **/
.page-section{
  margin-bottom: 20rpx;
}
.weui-cells__title {
  margin-top: .77em;
  margin-bottom: .3em;
  padding-left: 15px;
  padding-right: 15px;
  color: #999999;
  font-size: 14px;
}
.weui-cells_after-title {
  margin-top: 0;
}
.weui-cell_input {
  padding-top: 0;
  padding-bottom: 0;
}

实现效果:
在这里插入图片描述
在小程序中加载视频
js方法:

Page({
   onReady(res) {
    	// 通过 wx.createVideoContext创建实例
        this.videoCtx = wx.createVideoContext('myVideo')
    },
    })

wxml页面样式:
通过

<!--wxml-->
<video 
	id="myVideo" 
	class='center' 
	src="http://v1-default.ixigua.com/3e172136f7b6f24afa0542e95163c28f/5cf78470/video/m/2208e14fe501f214bf683571c5a6627f0b21145bd20000129652b3e5f1/?rc=Ojs3NDM1OzZpOTxoaDk4N0ApQHRAbzo5NTU8MzUzMzU4NDMzNDVvQGg0dilAZzN3KUBmM3UpZHNyZ3lrdXJneXJseHdmNDNALTEwNjIvLV8tMTYzNl80YnMtbyNvIzE0Ly0vNi0uNC4yMjM2LTojbyM6YS1vIzpgLXAjOmB2aVxiZitgXmJmK15xbDojLi9e" enable-danmu 
	danmu-btn 
	controls
/>

wcss样式文件

.center{
  width: 100%;
  height: 230px;  
  align-items: center;  
  justify-content: center;
}

将获取弹幕内容、视频播放控件两部分合并,实现弹幕发放的功能。
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({
    data:{      
    	inputValue: "",    
    	},    
    onReady(res) {        
    	this.videoCtx = wx.createVideoContext('myVideo')
    	},
    play() {        
    	this.videoCtx.play()    
    	},    
    pause() {        
    	this.videoCtx.pause()    
    	},
    	
     // 添加弹幕    
     bindInputBlur(e) {      
     	this.inputValue =  e.detail.value
     	this.setData({        
     		inputValue : e.detail.value      
     		})    
     },


     // 发送弹幕,并清空弹幕数据
     bindSendDanmu() {      
     	this.videoCtx.sendDanmu({        
     		text: this.inputValue,        
     		color: getRandomColor()      
     		})      
     	this.setData({        
     		inputValue: ''
     		})      
     	console.log(this.inputValue)
     	},  
     })

wxml文件:
在这里插入图片描述
wxss样式文件
在这里插入图片描述
展示效果:
在这里插入图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值