video

优质
小牛编辑
139浏览
2023-12-01

视频组件

属性类型默认值必填说明
srcstring要播放视频的资源地址
autoplaybooleanfalse是否自动播放,只在初始化时有效,不能动态变更
mutedbooleanfalse是否静音播放
controlsbooleantrue是否显示默认播放控件(播放/暂停按钮、播放进度、时间),仅安卓支持
loopbooleanfalse是否循环播放
bindplayeventHandler当开始/继续播放时触发play事件
bindpauseeventHandler当暂停播放时触发 pause 事件
bindendedeventHandler当播放到末尾时触发 ended 事件
bindtimeupdateeventHandler播放进度变化时触发
bindfullscreenchangeeventHandler视频进入和退出全屏时触发,回调参数需要有direction。有效值为 vertical 或 horizontal
binderroreventHandler视频播放出错时触发
bindwaitingeventHandler视频出现缓冲时触发
bindloadeddataeventHandler视频资源开始加载时触发
bindloadedmetadataeventHandler开始加载元数据时触发
bindloadstarteventHandler开始加载数据
bindseekedeventHandler拖动进度条结束
bindseekingeventHandler正在拖动进度条
objectFitstringcontain有效值contain fill ,只在初始化时有效,不能动态变更
initialTimenumber0指定视频初始播放位置
posterstring视频封面图片网络资源地址 (基础库1.10.12开始支持)
mobilenet-hint-typenumber1移动网络提醒样式:0是不提醒,1是提醒,默认值为1

支持的格式

支持格式iOS安卓
Mp4支持支持
mov支持支持
m4v支持支持
3gp不支持支持
avi支持不支持

示例代码

jxml

<view class="page-body">

    <video id="myVideo"
      src="http://storage.jd.com/videoares/%E6%8A%98%E5%8F%A0%E5%B1%8F%E9%97%AE%E9%A2%98.mp4"
      autoplay="{{autoplay}}"
      muted="{{muted}}"
      controls="{{controls}}"
      bindplay="play"
      bindpause="pauseHandler"
      bindended="endHandler"
      binderror="videoErrorCallback"
      objectFit="{{objectFit}}"
      initialTime="{{initialTime}}"
      poster="{{poster}}"
      mobilenet-hint-type="{{mobilenetHintType}}"
    ></video>
    <view class="page-control">
      <view class="page-control-row">
        <view class="page-control-title">initialTime</view>
        <input class="control-value" bindinput="initialTimeChanged" type="number" placeholder="在此处输入initialTime" value="{{initialTime}}"/>
      </view>
    </view>
    <view class="page-control">  
      <view class="page-control-sub-title">设置当前video objectFit</view>
      <view class="current-container">
        <radio-group bindchange="changeCurrent">
          <label class="default-label1"><radio value="contain" checked="{{true}}">contain</radio></label>
          <label class="default-label1"><radio value="fill" >fill</radio></label>
        </radio-group>
      </view>
    </view> 
    <view class="page-control">
      <view class="page-control-row">
        <view class="page-control-title">muted</view>
        <switch checked="{{muted}}" bindchange="changeMuted" />
      </view>
      <view class="page-control-row">
        <view class="page-control-title">loop</view>
        <switch checked="{{loop}}" bindchange="changeLoop" />
      </view>
    </view>
    <view class="page-control">
      <view class="page-control-row">
        <view class="page-control-title">controls(ios不支持)</view>
        <switch checked="{{controls}}" bindchange="changeControls"/>
       </view>
       <view class="page-control-row">
        <view class="page-control-title">poster</view>
        <input class="control-value" value="{{poster}}" maxlength='500' bindblur="bindInputBlur"/>
      </view>
    </view>
  </view>
</view>

js

Page({
  onReady: function (res) {
    this.videoContext = jd.createVideoContext('myVideo');
  },
  inputValue: '',
  data: {
    src: '',
    initialTime:0,
    objectFit:"contain",
    autoplay:false,
    muted:false,
    loop:false,
    controls:true,
    poster:'',
    mobilenetHintType:0,
  },
  bindInputBlur: function (e) {
    this.setData({
      poster: e.detail.value
    })
  },
  changeControls:function(){
    this.setData({
      controls:!this.data.controls
    })
  },
  changeCurrent:function(e) {
    this.setData({
      objectFit: e.detail.value
    });
  },
  initialTimeChanged :function(e){
    this.setData({
      initialTime: e.detail.value
    });
  },
  play:function(){
    console.log("点击播放按钮");
  },
  pauseHandler:function(){
    console.log("点击暂停按钮");
  },
  endHandler:function(){
    console.log("播放到末尾");
  },
  fullscreenchangeHandler:function(e){
    console.log('全屏变化:',e.detail);
  },
  videoErrorCallback: function (e) {
    console.log('视频错误信息:',e);
  },
  changeMuted:function(){
    this.setData({
      muted:!this.data.muted
    })
  },
  changeLoop:function(){
    this.setData({
      loop:!this.data.loop
    })
  }

});