微信小程序 - 发送视频弹幕

微信小程序 - 发送视频弹幕

 

微信小程序官方文档提供了发送视频弹幕的功能,参照其功能做了一些改动,实现可发送用户自定义颜色的弹幕。

 

function getRandomColor() {
    var rgb = [];
    for (var i = 0; i < 3; ++i) {
        var color = Math.floor(Math.random() * 256).toString(16);
        color = color.length == 1 ? '0' + color : color;
        rgb.push(color);
    }
    return '#' + rgb.join('');
}
Page({
    inputValue: '',
    data: {
        isRandomColor: true,
        src: '',
        numberColor: "#ff0000",
        danmuList: [{
            text: '第 1s 出现的红色弹幕',
            color: '#ff0000',
            time: 1
        }, {
            text: '第 2s 出现的绿色弹幕',
            color: '#00ff00',
            time: 2
        }]
    },
    onLoad: function onLoad() {
        var _this = this;
        wx.getSystemInfo({
            success: function success(res) {
                // video标签默认宽度300px、高度225px
                var windowWidth = res.windowWidth;                
                var videoHeight = 225 / 300 * windowWidth;
                _this.setData({
                    videoWidth: windowWidth,
                    videoHeight: videoHeight
                });
            }
        });
    },
    onReady: function onReady(res) {
        this.videoContext = wx.createVideoContext('myVideo');
    },
    onShow: function onShow() {
        var _this = this;
        wx.getStorage({
            key: 'numberColor',
            success: function success(res) {
                _this.setData({
                    numberColor: res.data
                });
            }
        });
    },
    bindInputBlur: function(e) {
        this.inputValue = e.detail.value;
    },
    bindSendDanmu: function() {
        if (this.data.isRandomColor) {
            var color = getRandomColor();
        } else {
            var color = this.data.numberColor;
        }
        this.videoContext.sendDanmu({
            text: this.inputValue,
            color: color
        });
    },
    videoErrorCallback: function(e) {
        console.log('视频错误信息:');
        console.log(e.detail.errMsg);
    },
    switchChange: function(e) {
        this.setData({
            isRandomColor: e.detail.value
        }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值