【愚公系列】2022年04月 微信小程序-视频播放

358 阅读5分钟

一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第11天,点击查看活动详情

前言

video视频播放相关属性:

属性类型默认值必填说明最低版本
srcstring要播放视频的资源地址,支持网络路径、本地临时路径、云文件ID(2.3.0)1.0.0
durationnumber指定视频时长1.1.0
controlsbooleantrue是否显示默认播放控件(播放/暂停按钮、播放进度、时间)1.0.0
danmu-listArray.弹幕列表1.0.0
danmu-btnbooleanfalse是否显示弹幕按钮,只在初始化时有效,不能动态变更1.0.0
enable-danmubooleanfalse是否展示弹幕,只在初始化时有效,不能动态变更1.0.0
autoplaybooleanfalse是否自动播放1.0.0
loopbooleanfalse是否循环播放1.4.0
mutedbooleanfalse是否静音播放1.4.0
initial-timenumber0指定视频初始播放位置1.6.0
page-gesturebooleanfalse在非全屏模式下,是否开启亮度与音量调节手势(废弃,见 vslide-gesture)1.6.0
directionnumber设置全屏时视频的方向,不指定则根据宽高比自动判断1.7.0
show-progressbooleantrue若不设置,宽度大于240时才会显示1.9.0
show-fullscreen-btnbooleantrue是否显示全屏按钮1.9.0
show-play-btnbooleantrue是否显示视频底部控制栏的播放按钮1.9.0
show-center-play-btnbooleantrue是否显示视频中间的播放按钮1.9.0
enable-progress-gesturebooleantrue是否开启控制进度的手势1.9.0
object-fitstringcontain当视频大小与 video 容器大小不一致时,视频的表现形式1.0.0
posterstring视频封面的图片网络资源地址或云文件ID(2.3.0)。若 controls 属性值为 false 则设置 poster 无效1.0.0
show-mute-btnbooleanfalse是否显示静音按钮2.4.0
titlestring视频的标题,全屏时在顶部展示2.4.0
play-btn-positionstringbottom播放按钮的位置2.4.0
enable-play-gesturebooleanfalse是否开启播放手势,即双击切换播放/暂停2.4.0
auto-pause-if-navigatebooleantrue当跳转到本小程序的其他页面时,是否自动暂停本页面的视频播放2.5.0
auto-pause-if-open-nativebooleantrue当跳转到其它微信原生页面时,是否自动暂停本页面的视频2.5.0
vslide-gesturebooleanfalse在非全屏模式下,是否开启亮度与音量调节手势(同 page-gesture)2.6.2
vslide-gesture-in-fullscreenbooleantrue在全屏模式下,是否开启亮度与音量调节手势2.6.2
ad-unit-idstring视频前贴广告单元ID,更多详情可参考开放能力视频前贴广告2.8.1
poster-for-crawlerstring用于给搜索等场景作为视频封面展示,建议使用无播放 icon 的视频封面图,只支持网络地址
show-casting-buttonbooleanfalse显示投屏按钮。安卓在同层渲染下生效,支持 DLNA 协议;iOS 支持 AirPlay 和 DLNA 协议2.10.2
picture-in-picture-modestring/Array设置小窗模式: push, pop,空字符串或通过数组形式设置多种模式(如: ["push", "pop"])2.11.0
picture-in-picture-show-progressbooleanfalse是否在小窗模式下显示播放进度2.11.0
enable-auto-rotationbooleanfalse是否开启手机横屏时自动全屏,当系统设置开启自动旋转时生效2.11.0
show-screen-lock-buttonbooleanfalse是否显示锁屏按钮,仅在全屏时显示,锁屏后控制栏的操作2.11.0
show-snapshot-buttonbooleanfalse是否显示截屏按钮,仅在全屏时显示2.13.0
show-background-playback-buttonbooleanfalse是否展示后台音频播放按钮2.14.3
background-posterstring进入后台音频播放后的通知栏图标(Android 独有)2.14.3
referrer-policystringno-referrer格式固定为 https://servicewechat.com/{appid}/{version}/page-frame.html,其中 {appid} 为小程序的 appid,{version} 为小程序的版本号,版本号为 0 表示为开发版、体验版以及审核版本,版本号为 devtools 表示为开发者工具,其余为正式版本;2.13.0
is-drmboolean是否是 DRM 视频源2.19.3
provision-urlstringDRM 设备身份认证 url,仅 is-drm 为 true 时生效 (Android)2.19.3
certificate-urlstringDRM 设备身份认证 url,仅 is-drm 为 true 时生效 (iOS)2.19.3
license-urlstringDRM 获取加密信息 url,仅 is-drm 为 true 时生效2.19.3
bindplayeventhandle当开始/继续播放时触发play事件1.0.0
bindpauseeventhandle当暂停播放时触发 pause 事件1.0.0
bindendedeventhandle当播放到末尾时触发 ended 事件1.0.0
bindtimeupdateeventhandle播放进度变化时触发,event.detail = {currentTime, duration} 。触发频率 250ms 一次1.0.0
bindfullscreenchangeeventhandle视频进入和退出全屏时触发,event.detail = {fullScreen, direction},direction 有效值为 vertical 或 horizontal1.4.0
bindwaitingeventhandle视频出现缓冲时触发1.7.0
binderroreventhandle视频播放出错时触发1.7.0
bindprogresseventhandle加载进度变化时触发,只支持一段加载。event.detail = {buffered},百分比2.4.0
bindloadedmetadataeventhandle视频元数据加载完成时触发。event.detail = {width, height, duration}2.7.0
bindcontrolstoggleeventhandle切换 controls 显示隐藏时触发。event.detail = {show}2.9.5
bindenterpictureinpictureeventhandler播放器进入小窗2.11.0
bindleavepictureinpictureeventhandler播放器退出小窗2.11.0
bindseekcompleteeventhandlerseek 完成时触发 (position iOS 单位 s, Android 单位 ms)

orientation子属性:

合法值说明
0正常竖向
90屏幕逆时针90度
-90屏幕顺时针90度

object-fit子属性:

合法值说明
contain包含
fill填充
cover覆盖

play-btn-position子属性:

合法值说明
bottomcontrols bar上
center视频中间

picture-in-picture-mode子属性:

合法值说明
[]取消小窗
push路由 push 时触发小窗
pop路由 pop 时触发小窗

referrer-policy子属性:

合法值说明
origin发送完整的referrer
no-referrer不发送

一、视频播放

1.js代码

function getRandomColor () {
  const 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({
  onReady: function (res) {
    this.videoContext = wx.createVideoContext('myVideo')
  },
  inputValue: '',
    data: {
    src: '',
    danmuList:
      [{
        text: '第 1s 出现的弹幕',
        color: '#ff0000',
        time: 1
      },
      {
        text: '第 3s 出现的弹幕',
        color: '#ff00ff',
        time: 3
      }]
    },
  bindInputBlur: function(e) {
    this.inputValue = e.detail.value
  },
  bindSendDanmu: function () {
    this.videoContext.sendDanmu({
      text: this.inputValue,
      color: getRandomColor()
    })
  },
  bindPlay: function() {
    this.videoContext.play()
  },
  bindPause: function() {
    this.videoContext.pause()
  },
  videoErrorCallback: function(e) {
    console.log('视频错误信息:')
    console.log(e.detail.errMsg)
  }
})

2.wxml代码

<view class="page-body">
  <view class="page-section tc">
    <video id="myVideo" src="http://wxsnsdy.tc.qq.com/105/20210/snsdyvideodownload?filekey=30280201010421301f0201690402534804102ca905ce620b1241b726bc41dcff44e00204012882540400&bizid=1023&hy=SH&fileparam=302c020101042530230204136ffd93020457e3c4ff02024ef202031e8d7f02030f42400204045a320a0201000400" binderror="videoErrorCallback" danmu-list="{{danmuList}}" enable-danmu danmu-btn controls></video>

    <view class="weui-cells">
      <view class="weui-cell weui-cell_input">
        <view class="weui-cell__hd">
          <view class="weui-label">弹幕内容</view>
        </view>
        <view class="weui-cell__bd">
          <input bindblur="bindInputBlur" class="weui-input" type="text" placeholder="在此处输入弹幕内容" />
        </view>
      </view>
    </view>
    <view class="btn-area">
      <button bindtap="bindSendDanmu" class="page-body-button" type="primary" formType="submit">发送弹幕</button>
      <button bindtap="bindPlay" class="page-body-button" type="primary">播放</button>
      <button bindtap="bindPause" class="page-body-button" type="primary">暂停</button>
    </view>
  </view>
</view>

3.WXSS

@import "../lib/weui.wxss";

.weui-cells{
  margin-top: 80rpx;
  text-align: left;
}
.weui-label{
  width: 5em;
}

.page-body-button {
  margin-bottom: 30rpx;
}

weui为第三方ui可以百度搜索下载

4.效果

在这里插入图片描述