小程序live-pusher与live-player网络断开监听以及恢复推拉流

主播端监听异常以及恢复live-pusher

这里是错误码
这里是绑定方法

	<live-pusher id="pusher" url="{{pushURL}}" mode="{{pushMode}}" bindstatechange="statechange" beauty="{{magic}}" whiteness="{{magic}}" enable-mic="{{isEnableMic}}" />

  onLoad: function (options) {
  		//	创建 live-pusher 上下文 LivePusherContext 对象。
		this.pushContext = wx.createLivePusherContext()
  },
  statechange() {
    switch (e.detail.code) {
      case -1307:
      // 	监听到了直播网络断开
        wx.showModal({
          content: '网络连接异常',
          confirmText:'继续直播',
          success:() => {
          // 这里是重新开启推流 
           	 if(res.confirm) {
               this.pushContext.start()
             } else {
               // 取消处理
			 }
           }
        })
        break;
      case 3004:
        wx.showToast({
          title: '远程服务器主动断开连接',
          icon: 'none'
        });
        break;
      default:
        break;
    }
  }
  

用户端监听异常以及恢复live-player

这里是绑定方法
这里是错误码

<live-player id="player" src="{{pullUrl}}" mode="live"  bindstatechange="statechange" binderror="error" autoplay="true" object-fit="fillCrop" picture-in-picture-mode='{{picture}}' muted="{{pullUrl==''?true:false}}"
  />
  onLoad: function (options) {
  		//	创建 live-player 上下文 LivePlayerContext 对象。 这里要绑定id
		this.PlayerContext = wx.createLivePlayerContext('player')
  },
  statechange() {
    switch (e.detail.code) {
      case -2301:
      // 注意 用户自己断开网络和 主播断开网络都会走 -2301的错误码
        wx.getNetworkType({
          success: res => {
            console.log(res.networkType)
            // 判断用户自己 是否有网络 有就提示用户自己网络异常,没有就是主播的网络异常
            if(res.networkType != "none") {
              wx.showModal({
                title: '错误',
                content: '主播网络不佳,正在努力恢复',
                showCancel:false,
                success: res => {
                // 恢复拉流的画面
                  this.PlayerContext.play()
                }
              })
            }else {
              wx.showModal({
                title: '错误',
                content: '请检查你的网络连接是否正常',
                showCancel:false,
                success: res => {
                // 恢复拉流的画面
                  this.PlayerContext.play()
                }
              })
            }
          }
        })
        break;
      case 3004:
        wx.showToast({
          title: '远程服务器主动断开连接',
          icon: 'none'
        });
        break;
      default:
        break;
    }
  }
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值