index.wxml
<view class="maxWidth">
<video src="http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4" controls enable-danmu danmu-btn
danmu-list="{{danmuList}}" style="width: 100%;" id="videoTest"></video>
</view>
<view>
<input type="text" placeholder="请输入弹幕" bindblur="bindInputblur" class="inputStyle" value="{{content}}"/>
<button type = "primary" bindtap="bindSendDanmu">发送弹幕</button>
<block wx:for="{{danmuList}}">
<text>{{item.text}} \n</text>
</block>
</view>
index.js
// index.js
// 获取应用实例
const app = getApp()
Page({
videoContext:null,
inputValue:'',
onReady(){
this.videoContext = wx.createVideoContext('videoTest')
},
data:{
danmuList:[
{text:'测试'}
],
content:''
},
bindInputblur(e){
this.data.inputValue = e.detail.value
},
bindSendDanmu(){
this.setData({
danmuList:this.data.danmuList.concat({
text:this.data.inputValue
}),
content:''
})
this.videoContext.sendDanmu({
text:this.data.inputValue,
color:'#FF0000'
})
console.log(this.data.danmuList);
}
})