微信小程序调用视频流媒体,实现视频全屏切换及暂停播放

  1. 业务场景描述:适用于在微信小程序中调用视频流媒体服务加载显示直播流

  2. 前期设置,登录微信公众平台,调用视频只针对如下类目小程序开放
    在这里插入图片描述
    需在设置-服务类目中设置

  3. 开通实时播放音视频流接口
    选择开发-接口设置-实时播放音视频接口在这里插入图片描述

  4. 在绑定appId的小程序中调用加载显示直播流
    video.xml代码

<live-player id="player" class='{{winstyle}}' src="直播流路径" mode="RTC" autoplay="true" bindstatechange="statechange" binderror="error">
    <cover-view class="controls">
    <cover-view class="play" bindtap="bindFullScreen">
    	<!--全屏图片-->
      <cover-image class="img" src="/images/full.png" />
    </cover-view>
    <cover-view class="pause" bindtap="bindExitFullScreen">
    	<!--退出全屏图片-->
      <cover-image class="img" src="/images/exitFull.png" />
    </cover-view>
    <cover-view class="time">00:00</cover-view>
  </cover-view>
</live-player>

加载显示视频

// pages/video/video.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    state: 0, // 当前视频状态,IOS真机测试navigateBack后showToast Toast不显示。0暂停,1播放,-1无视频或播放失败,用于控制页面上元素的展示
  },
  play: function() {
    this.videoCtx.play()
  },
  pause: function() {
    this.videoCtx.pause()
  },
  onReady(res) {

    this.ctx = wx.createLivePlayerContext('player')

  },
  // 监听状态改变
  statechange(e) {

    if (e.detail.code == '-2301') {

      console.log('loading fail');

      this.setData({

        state: -1

      });

    }

    console.log('live-player code:', e.detail.code)

  },
  // 错误处理
  error(e) {

    console.error('live-player error:', e.detail.errMsg);

    this.setData({

      state: -1

    });

  },

  bindPlay() {

    this.ctx.play({

      success: res => {

        this.setData({

          // state:1 - this.data.state // 1

          state: 1

        });

        console.log('play success')

      },

      fail: res => {

        console.log('play fail');

        this.setData({

          state: -1

        });

      }

    })

  },

  bindPause() {

    this.ctx.pause({

      success: res => {

        this.setData({

          // state:1 - this.data.state // 0

          state: 0

        });

        console.log('pause success')

      },

      fail: res => {

        console.log('pause fail')

        this.setData({

          state: -1

        });

      }

    })

  },

  bindFullScreen() {

    this.ctx.exitFullScreen({

      success: res => {

        console.log('exitFullScreen success')

      },

      fail: res => {

        console.log('exitFullScreen fail')

        this.setData({

          state: -1

        });

      }

    })

  },

  bindExitFullScreen() {

    this.ctx.requestFullScreen({

      direction: 90,

      success: res => {

        console.log('requestFullScreen success')

      },

      fail: res => {

        console.log('requestFullScreen fail')

        this.setData({

          state: -1

        });

      }

    })

  },
})
  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值