主播端监听异常以及恢复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;
}
}