微信小程序> 小程序直播页面遮罩及按钮设置

小程序直播页面遮罩及按钮设置

浏览量:3670 时间: 来源:神盾局的猿猿

开发之路,羊肠九曲,荆棘密布,幸得高人指点,前辈填坑,一路谨小慎微,终得工程圆满;其间填坑之经验,开路之历程,皆为精华,不可弃之;记录于此,以便事后回顾,亦想于有相关开发疑惑之同学做参考之用,文中如若有错,恳请雅正,不胜感激。

小程序的直播页面,一般我们都会遇到待直播状态时的处理,即对组件进行遮罩。

map、video、canvas、camera、live-player、live-pusher等组件,一般图片或其他组件是无法覆盖的,只能使用cover-view组件 和cover-image组件

另外一般我们都需要处理全屏/非全屏,如果只是使用同一组按钮,只能改变样式来控制按钮状态及位置,但是实际开发过程中会发现不太现实,所以我们使用两套按钮及背景来做。

闲话少叙,直接上代码

<live-player  id="video-livePlayer" class="video-livePlayer" autoplay="{{playing}}" mode="live" orientation="{{orientation}}" muted="{{muted}}" background-mute="{{backgroundMute}}" object-fit="{{objectFit}}" min-cache="1" max-cache="3" src="{{playUrl}}" debug="{{debug}}" bindstatechange="onPlayEvent"  bindfullscreenchange="onFullScreenChange" binderror="error" catchtap="showTools">  <!-- 非全屏状态下的背景遮罩 -->  <cover-view wx:if="{{!fullScreen}}"  style="width:100%; height: 50px; position:absolute; bottom:0px;">    <cover-image class="toolbg" src="../../images/video_bg.png"></cover-image>    <!-- 非全屏状态下的直播按钮 -->    <cover-view class='bottom_box minplaybtn'>      <cover-image class="bottom_button" src="../../images/{{playing?'pend':'play'}}.png" bindtap='onPlayClick'></cover-image>    </cover-view>    <cover-view class='bottom_box minFullbtn' bindtap='onFullScreenClick'>      <cover-image class="bottom_button" src="../../images/{{fullScreen?'min':'full'}}.png" ></cover-image>    </cover-view>  </cover-view>  <!-- 全屏状态下的背景遮罩 -->  <cover-view wx:if="{{fullScreen}}" hidden="{{hideTool}}" class="fullbtngroup">    <cover-image class="toolbg" src="../../images/video_bg2.png"></cover-image>    <!-- 全屏状态下的直播按钮 -->    <cover-view class='bottom_box minplaybtn2 tran'>      <cover-image class="bottom_button" src="../../images/{{playing?'pend':'play'}}.png" bindtap='onPlayClick'></cover-image>    </cover-view>    <cover-view class='bottom_box minFullbtn2'>      <cover-image class="bottom_button" src="../../images/{{fullScreen?'min':'full'}}.png" bindtap='onFullScreenClick'></cover-image>    </cover-view>  </cover-view></live-player>

js 文件

//创建直播  createContext: function () {    this.setData({      videoContext: wx.createLivePlayerContext("video-livePlayer")    })    // this.data.videoContext.requestFullScreen({    //   direction: 0,    // })  },  // 直播播放  onPlayClick: function () {    var url = this.data.playUrl;    if (url.indexOf("rtmp:") == 0) {    } else if (url.indexOf("https:") == 0 || url.indexOf("http:") == 0) {      if (url.indexOf(".flv") != -1) {      }    } else {      wx.showToast({        title: '播放地址不合法,目前仅支持rtmp,flv方式!',        icon: 'loading',      })    }this.setData({  playing: !this.data.playing,  hidePlay:true,  hideTool: true,})if (this.data.playing) {  this.data.videoContext.play();  console.log("video play()");  wx.showLoading()} else {  this.data.videoContext.stop();  console.log("video stop()");  wx.hideLoading();  this.setData({    hidePlay:false  })}},// 横屏onFullScreenClick: function () {if (!this.data.fullScreen) {  this.data.videoContext.requestFullScreen({    direction: 0,  })  this.data.orientation = "horizontal";} else {  this.data.videoContext.exitFullScreen({  })  this.data.orientation = "vertical";}this.setData({  orientation: this.data.orientation})  }, //播放状态事件  onPlayEvent: function (e) {    console.log(e.detail.code);    if (e.detail.code == -2301) {      this.stop();      this.setData({        hidePlay:false,        hideTool:false      })      wx.showToast({        title: '拉流多次失败',      })    }    if (e.detail.code == 2004) {      wx.hideLoading();    }  },  // 全屏  onFullScreenChange: function (e) {    this.setData({      fullScreen: e.detail.fullScreen    })    console.log(e);    wx.showToast({      title: this.data.fullScreen ? '全屏' : '退出全屏',    })  },  //停止直播  stop: function () {    if (this.data.fullScreen) {      this.data.videoContext.exitFullScreen({  })  this.setData({    orientation: "vertical"  })}this.setData({  playing: false,  playUrl: "rtmp://",  orientation: "vertical",  objectFit: "contain",  muted: false,  fullScreen: false,  backgroundMuted: false,  debug: false,  exterFlag: false,})this.data.videoContext.stop();wx.hideLoading();  },

其他代码具体项目具体开发,以上贴出的只是最主要做基本的部分,如开发中有需要帮助,可添加微信“Founder311”。

版权声明

即速应用倡导尊重与保护知识产权。如发现本站文章存在版权问题,烦请提供版权疑问、身份证明、版权证明、联系方式等发邮件至197452366@qq.com ,我们将及时处理。本站文章仅作分享交流用途,作者观点不等同于即速应用观点。用户与作者的任何交易与本站无关,请知悉。

产品经理

手机 : 13312967497

擅长 : 小程序流量变现

扫码领取礼包

最新资讯

热门模板

  • 头条
  • 搜狐
  • 微博
  • 百家
  • 一点资讯
  • 知乎