微信小程序Video组件实现视频、自定义播放按钮、封面图、封面图文字demo

2022-12-15 17:08:19 浏览数 (2)

效果图

实现思路

在video标签中添加view或cover-view标签,封面图可直接设置video组件的poster属性,自定义按钮和封面图文字包在view中设置定位即可,给自定义按钮绑定点击事件,触发事件后隐藏最外层view,给video绑定bindended事件 设置最外层view显示

wxml

代码语言:javascript复制
<video id="myVideo" src="{{videoSrc}}" enable-danmu controls show-fullscreen-btn show-center-play-btn="{{false}}" auto-pause-if-navigate auto-pause-if-open-native show-play-btn object-fit="fill" bindpause="bindpause" bindended="bindended" >
  <cover-view class="video_cover" wx:if="{{isShow}}" bindtap="bindplay">
    <cover-image src="{{videoCoverImg}}" mode="widthFix"></cover-image>
    <cover-image src="{{videoPlayIcon}}" mode="widthFix" class="video_play_icon"></cover-image>
    <cover-view class="video_bg_black">
      <cover-view class="video_cover_txt">
        <cover-view>
          <cover-view class="video_txt">1天1元,开通VIP</cover-view>
          <cover-view class="video_txt">更多优质视频等你来解锁</cover-view>
        </cover-view>
        <cover-image src="{{videoLockIcon}}" class="video_icon_lock" mode="widthFix"></cover-image>
      </cover-view>
    </cover-view>      
  </cover-view>
</video>
代码语言:javascript复制
.video_cover{
  width:100%;
  height:100%;
  position:relative;
}
.video_play_icon{
  position:absolute;
  left:50%;
  top:50%;
  transform:translate(-50%,-50%);
  width:40px;
  height:40px;
  z-index:5
}
.video_bg_black{
  position:absolute;
  left:0;
  top:0;
  bottom:0;
  right:0;
  background:rgba(0,0,0,.5);
}
.video_cover_txt{
  display:flex;
  justify-content: flex-end;
  align-items: center;
  text-align:right;
  color: #fff;
  font-size: 28rpx;
  margin-top: 35rpx;
  height: 90rpx;
}
.video_icon_lock{
  width:30rpx;
  height: auto;
  margin:0 30rpx;
}
.video_txt{
  margin:10rpx auto;
}

js

代码语言:javascript复制
Page({
  data: {
      isShow:true,
      videoSrc:'http://wxsnsdy.tc.qq.com/105/20210/snsdyvideodownload?filekey=30280201010421301f0201690402534804102ca905ce620b1241b726bc41dcff44e00204012882540400&bizid=1023&hy=SH&fileparam=302c020101042530230204136ffd93020457e3c4ff02024ef202031e8d7f02030f42400204045a320a0201000400',   // 视频
      videoCoverImg:'http://img5.imgtn.bdimg.com/it/u=1672477765,2527992874&fm=26&gp=0.jpg',   // 视频封面图
      videoPlayIcon:'http://39.105.134.221:8080/test/source1.png',  // 视频播放icon
      videoLockIcon:'http://39.105.134.221:8080/test/source2.png',   // 视频播放锁
  },
  onReady: function () {
    this.videoContext = wx.createVideoContext('myVideo')
  },
  // 点击封面自定义播放按钮时触发
  bindplay() {
    this.setData({
      isShow:false
    }
    this.videoContext.play();
    console.log('play')
  },
  // 监听播放到末尾时触发
  bindended(){
    console.log('bindended')
    this.setData({
      isShow: true
    })
    this.videoContext.ended();
  },
  // 监听暂停播放时触发
  bindpause(){
    console.log('pause')
  }
})

0 人点赞