微信小程序 video 视频 组件

完整微信小程序(Java后端) 技术贴目录清单页面(必看)

视频(v2.4.0 起支持同层渲染)。相关api:wx.createVideoContext

属性类型默认值必填说明最低版本
srcstring要播放视频的资源地址,支持网络路径、本地临时路径、云文件ID(2.3.01.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
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)2.12.0

direction 的合法值

说明最低版本
0正常竖向
90屏幕逆时针90度
-90屏幕顺时针90度

object-fit 的合法值

说明最低版本
contain包含
fill填充
cover覆盖

play-btn-position 的合法值

说明最低版本
bottomcontrols bar上
center视频中间

picture-in-picture-mode 的合法值

说明最低版本
[]取消小窗
push路由 push 时触发小窗
pop路由 pop 时触发小窗

Bug & Tip

  1. tip:`video 默认宽度 300px、高度 225px,可通过 wxss 设置宽高。
  2. tip:从 2.4.0 起 video 支持同层渲染,更多请参考原生组件使用限制

支持的格式

格式iOSAndroid
mp4
movx
m4vx
3gp
avix
m3u8
webmx

支持的编码格式

格式iOSAndroid
H.264
HEVC
MPEG-4
VP9x

小窗特性说明

video 小窗支持以下三种触发模式(在组件上设置 picture-in-picture-mode 属性):

  1. push 模式,即从当前页跳转至下一页时出现小窗(页面栈push)
  2. pop 模式,即离开当前页面时触发(页面栈pop)
  3. 以上两种路由行为均触发小窗

此外,小窗还支持以下特性:

  • 小窗容器尺寸会根据原组件尺寸自动判断
  • 点击小窗,用户会被导航回小窗对应的播放器页面
  • 小窗出现后,用户可点击小窗右上角的关闭按钮或调用 context.exitPictureInPicture() 接口关闭小窗

当播放器进入小窗模式后,播放器所在页面处于 hide 状态(触发 onHide 生命周期),该页面不会被销毁。当小窗被关闭时,播放器所在页面会被 unload (触发 onUnload 生命周期)。

示例代码

在开发者工具中预览效果

index.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>

index.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)
  }
})

运行效果:

在这里插入图片描述

微信搜一搜【java1234】关注这个放荡不羁的程序员,关注后回复【资料】有我准备的一线大厂笔试面试资料以及简历模板。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值