微信小程序实现简单的视频播放

横屏

竖屏

布局代码:

<!--pages/live/live.wxml   src="{{channels[currentIndex].playUrl}}"-->
<!--视频播放器,属性设置参考:https://developers.weixin.qq.com/miniprogram/dev/component/video.html-->
<view>
	<video show-mute-btn="{{true}}" 
	src="{{channels[currentIndex].playUrl}}"
	picture-in-picture-mode="{{['push', 'pop']}}" show-progress="{{false}}" autoplay="{{true}}" show-casting-button="{{true}}" show-fullscreen-btn="{{true}}" show-screen-lock-button="{{true}}" enable-auto-rotation="{{true}}" enable-progress-gesture="{{false}}" bindenterpictureinpicture='bindVideoEnterPictureInPicture' bindleavepictureinpicture='bindVideoLeavePictureInPicture' bindfullscreenchange="screenChange"></video>

	<image src="{{arrowImg}}" style="right:{{showChannels?130:0}}px" bindtap="onArrowClick" class="arrow"></image>

	<!--频道列表,全屏时video层级最高,显示不了其他组件,所以全屏时暂不能显示频道列表-->
	<view class="channel_root">
		<scroll-view scroll-y="{{true}}" class="channel_scroll" style="display:{{showChannels?'block':'none'}};">
			<view bindtap="onChannelClick" data-position="{{index}}" wx:for="{{channels}}">
				<view class="channel_item {{index === currentIndex?'active_item':''}}"><text>{{item.title}}</text></view>
			</view>
		</scroll-view>
	</view>
</view>

js代码:

// pages/live/live.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentIndex:0,
    channels:[{"imgUrl":"http://125.210.122.251:8080/IP/uploadChannelImg/542.png","title":"中央十套","playUrl":"http://cctvcnch5c.v.wscdns.com/live/cctv10_2/index.m3u8"},{"imgUrl":"http://125.210.122.251:8080/IP/uploadChannelImg/542.png","title":"中央十二套","playUrl":"http://cctvcnch5c.v.wscdns.com/live/cctv12_2/index.m3u8"},{"imgUrl":"http://125.210.122.251:8080/IP/uploadChannelImg/542.png","title":"中央十套","playUrl":"http://cctvcnch5c.v.wscdns.com/live/cctv10_2/index.m3u8"},{"imgUrl":"http://125.210.122.251:8080/IP/uploadChannelImg/542.png","title":"中央十二套","playUrl":"http://cctvcnch5c.v.wscdns.com/live/cctv12_2/index.m3u8"},{"imgUrl":"http://125.210.122.251:8080/IP/uploadChannelImg/542.png","title":"中央十套","playUrl":"http://cctvcnch5c.v.wscdns.com/live/cctv10_2/index.m3u8"}],
    showChannels:true,
    arrowImg:"img/ic_arrow_right.png"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  onChannelClick(e){
    if(e.currentTarget.dataset.position == this.data.currentIndex){
      return
    }
    this.setData({
      currentIndex:e.currentTarget.dataset.position
    })
    console.log(e)
  },
  
  onArrowClick(e){
    if(this.data.showChannels){
      this.setData({
        showChannels:false,
        arrowImg:"img/ic_arrow_left.png"
      })
    } else {
      this.setData({
        showChannels:true,
        arrowImg:"img/ic_arrow_right.png"
      })
    }
  },
  bindVideoEnterPictureInPicture() {
    console.log('进入小窗模式')
  },

  bindVideoLeavePictureInPicture() {
    console.log('退出小窗模式')
  },

  screenChange(res){
    console.log("screenChange->res:" + JSON.stringify(res))

  }
})

样式文件代码:

/* pages/live/live.wxss */
.channels {
  background: argb(255, 255, 255, 112);
}

video {
  width: 100%;
}

.channel_root {
  display: flex;
  flex-direction: row;
  z-index: 2;
  height: 225px;
  position: absolute;
  right: 0px;
  top: 0px;
  float: right;
}

.arrow {
  width: 20px;
  height: 30px;
  margin-top: 130rpx;
  padding-top: 30px;
  padding-bottom: 30px;
  padding-left: 5px;
  padding-right: 5px;
  position: absolute;
  z-index: 2;
}

.channel_root>.channel_scroll {
  background: rgba(255, 255, 255, 0.5);
  width: 130px;
  height: 225px;
}

.channel_item {
  margin-left: 15rpx;
  margin-top: 15rpx
}
.active_item{
  color: #ff0000;
}

json文件代码(不添加{}会报xpecting ‘STRING’,‘NUMBER’,‘NULL’,‘TRUE’,‘FALSE’,’{’,’[’, got EOF错误):

{}

左右箭头图片:
在这里插入图片描述
在这里插入图片描述

项目地址:https://gitee.com/load_star/wechatfuncs

  • 5
    点赞
  • 92
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值