温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在微信小程序开发中实现一个视频弹幕发送功能

发布时间:2020-12-28 14:56:31 阅读:534 作者:Leah 栏目:开发技术
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

本篇文章为大家展示了怎么在微信小程序开发中实现一个视频弹幕发送功能,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

1. 目录结构

怎么在微信小程序开发中实现一个视频弹幕发送功能

2. videoDanmu

<!-- 视频区域 -->
<video src="{{src}}"
id='video'
danmu-btn
enable-danmu
danmu-list="{{ danmuList }}"
>
</video>

<!-- 弹幕发送区域 -->
<view class='sendDanmu'>
  <textarea name="" id="" cols="30" rows="10" placeholder="请输入弹幕"
  bindinput="handleDanmuContent" 
  ></textarea>
  <button type='primary' bindtap="handleSendDanmu"> 发送 </button>
</view>

<!-- 是否随机颜色 -->
<view class='selectColor'>
  <text> 随机颜色 </text>
  <switch bindchange='handleChange'></switch>
</view>

<!-- 选择默认颜色 -->

<view class='selectDefaultColor' bindtap="handleGotoSelectColor">
  <text> 选择颜色 </text>
  <view ></view>
</view>
/* pages/videoDanmu/videoDanmu.wxss */

#video{
  width100%;
  heightcalc(750rpx * 225 / 300);
}

.sendDanmu{
  display: flex;
  height100rpx;
}

.sendDanmu textarea{
  border1px solid #ddd;
  height100rpx;
}

.selectColor{
  display: flex;
  justify-content: space-between;
  margin-top20rpx;
  border1px solid #ddd;
  align-items: center;
}

.selectDefaultColor{
  display: flex;
  justify-content: space-between;
  align-items: center;
  border1px solid #ddd;
  margin-top20rpx;
  margin-right30rpx;
}

.selectDefaultColor view{
  width50rpx;
  height50rpx;
  background-color: red;
}
// pages/videoDanmu/videoDanmu.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 视频地址
    src:"http://wxsnsdy.tc.qq.com/105/20210/snsdyvideodownload?filekey=30280201010421301f0201690402534804102ca905ce620b1241b726bc41dcff44e00204012882540400&bizid=1023&hy=SH&fileparam=302c020101042530230204136ffd93020457e3c4ff02024ef202031e8d7f02030f42400204045a320a0201000400",
    danmuList:
    [{
     text'第 1s 出现的弹幕',
     color'#ff0000',
     time1
    }, {
     text'第 3s 出现的弹幕',
     color'#ff00ff',
     time3
    }], // 初始弹幕列表
    defaultColor'red'// 弹幕默认颜色
    isRandomColorfalse// 控制弹幕是否随机颜色

  },

  // 获取弹幕内容
  handleDanmuContent(e){
    this.content = e.detail.value;

  },
  // 随机弹幕颜色
  randomColor(){
    let red = Math.random() * 255;
    let green = Math.random() * 255;
    let blue = Math.random() * 255;
    return `rgb(${red}${green}${blue})`
  },
  // 发送弹幕
  handleSendDanmu(){
    let color = '';
    // 通过状态值判断是默认颜色还是随机颜色
    if(this.data.isRandomColor){
      color = this.randomColor()
    }else{
      color = this.data.defaultColor
    }
    this.videoContext.sendDanmu({
      textthis.content,
      color
     })
  },
  // 处理弹幕是否随机颜色
  handleChange(e){
    // console.log(e.detail.value)
    this.setData({
      isRandomColor: e.detail.value
    })
  },
  // 跳转到选择颜色页面
  handleGotoSelectColor(){
    wx.navigateTo({
     url"/pages/selectColor/selectColor",
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReadyfunction () {
    // 创建 video 上下文 VideoContext 对象。通过这个对象发送弹幕
    this.videoContext = wx.createVideoContext('video'this)
  }
})

3. selectColor

<view class='color-wrap'>
  <view wx:for="{{ color }}" wx:key='index'
  
  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{
  width100rpx;
  height100rpx;
  background-color: red;
  margin-top20rpx;
}
// pages/selectColor/selectColor.js
import color from './color'
Page({

  /**
   * 页面的初始数据
   */
  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({
     delta0,
    })
  }
})
export default [
  { key1color' 白色 'number'#FFFFFF' },
 
  { key2color' 红色 'number'#FF0000' },
 
  { key3color' 绿色 'number'#00FF00' },
 
  { key4color' 蓝色 'number'#0000FF' },
 
  { key5color' 牡丹红 'number'#FF00FF' },
 
  { key6color' 青色 'number'#00FFFF' },
 
  { key7color' 黄色 'number'#FFFF00' },
 
  { key8color' 黑色 'number'#000000' },
 
  { key9color' 海蓝 'number'#70DB93' },
 
  { key10color' 巧克力色 'number'#5C3317' },
 
  { key11color' 蓝紫色 'number'#9F5F9F' },
 
  { key12color' 黄铜色 'number'#B5A642' },
 
  { key13color' 亮金色 'number'#D9D919' },
 
  { key14color' 棕色 'number'#A67D3D' },
 
  { key15color' 青铜色 'number'#8C7853' },
 
  { key16color' 2号青铜色 'number'#A67D3D' },
 
  { key17color' 士官服蓝色 'number'#5F9F9F' },
 
  { key18color' 冷铜色 'number'#D98719' },
 
  { key19color' 铜色 'number'#B87333' },
 
  { key20color' 珊瑚红 'number'#FF7F00' },
 
  { key21color' 紫蓝色 'number'#42426F' },
 
  { key22color' 深棕 'number'#5C4033' },
 
  { key23color' 深绿 'number'#2F4F2F' },
 
  { key24color' 深铜绿色 'number'#4A766E' },
 
  { key25color' 深橄榄绿 'number'#4F4F2F' },
 
  { key26color' 深兰花色 'number'#9932CD' },
 
  { key27color' 深紫色 'number'#871F78' },
 
  { key28color' 深石板蓝 'number'#6B238E' },
 
  { key29color' 深铅灰色 'number'#2F4F4F' },
 
  { key30color' 深棕褐色 'number'#97694F' },
 
  { key32color' 深绿松石色 'number'#7093DB' },
 
  { key33color' 暗木色 'number'#855E42' },
 
  { key34color' 淡灰色 'number'#545454' },
 
  { key35color' 土灰玫瑰红色 'number'#856363' },
 
  { key36color' 长石色 'number'#D19275' },
 
  { key37color' 火砖色 'number'#8E2323' },
 
  { key38color' 森林绿 'number'#238E23' },
 
  { key39color' 金色 'number'#CD7F32' },
 
  { key40color' 鲜黄色 'number'#DBDB70' },
 
  { key41color' 灰色 'number'#C0C0C0' },
 
  { key42color' 铜绿色 'number'#527F76' },
 
  { key43color' 青黄色 'number'#93DB70' },
 
  { key44color' 猎人绿 'number'#215E21' },
 
  { key45color' 印度红 'number'#4E2F2F' },
 
  { key46color' 土黄色 'number'#9F9F5F' },
 
  { key47color' 浅蓝色 'number'#C0D9D9' },
 
  { key48color' 浅灰色 'number'#A8A8A8' },
 
  { key49color' 浅钢蓝色 'number'#8F8FBD' },
 
  { key59color' 浅木色 'number'#E9C2A6' },
 
  { key60color' 石灰绿色 'number'#32CD32' },
 
  { key61color' 桔黄色 'number'#E47833' },
 
  { key62color' 褐红色 'number'#8E236B' },
 
  { key63color' 中海蓝色 'number'#32CD99' },
 
  { key64color' 中蓝色 'number'#3232CD' },
 
  { key65color' 中森林绿 'number'#6B8E23' },
 
  { key66color' 中鲜黄色 'number'#EAEAAE' },
 
  { key67color' 中兰花色 'number'#9370DB' },
 
  { key68color' 中海绿色 'number'#426F42' },
 
  { key69color' 中石板蓝色 'number'#7F00FF' },
 
  { key70color' 中春绿色 'number'#7FFF00' },
 
  { key71color' 中绿松石色 'number'#70DBDB' },
 
  { key72color' 中紫红色 'number'#DB7093' },
 
  { key73color' 中木色 'number'#A68064' },
 
  { key74color' 深藏青色 'number'#2F2F4F' },
 
  { key75color' 海军蓝 'number'#23238E' },
 
  { key76color' 霓虹篮 'number'#4D4DFF' },
 
  { key77color' 霓虹粉红 'number'#FF6EC7' },
 
  { key78color' 新深藏青色 'number'#00009C' },
 
  { key79color' 新棕褐色 'number'#EBC79E' },
 
  { key80color' 暗金黄色 'number'#CFB53B' },
 
  { key81color' 橙色 'number'#FF7F00' },
 ];

上述内容就是怎么在微信小程序开发中实现一个视频弹幕发送功能,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI

开发者交流群×