live-player中在onHide中操作直播实例不生效
<view class=“page-body”>
<view class=“page-section tc”>
<live-player id=“player” src="{{liveSrc}}"
mode=“live” bindstatechange=“statechange” binderror=“error” background-mute/>
<view class=“btn-area”>
<button bindtap=“bindPlay” class=“page-body-button” type=“primary”>播放</button>
<button bindtap=“bindPause” class=“page-body-button” type=“primary”>暂停</button>
<button bindtap=“bindStop” class=“page-body-button” type=“primary”>停止</button>
<button bindtap=“bindResume” class=“page-body-button” type=“primary”>恢复</button>
<button bindtap=“bindMute” class=“page-body-button” type=“primary”>静音</button>
<button bindtap=“setSrc” class=“page-body-button” type=“primary”>src</button>
</view>
</view>
</view>
/* eslint-disable */
const app = getApp()
Page({
data: {
liveSrc: “”,
state: 0, // 当前视频状态,0暂停,1播放,-1无视频或播放失败,用于控制页面上元素的展示
fullScreen: false,
mute: false,
init: true // 是否是第一次播放,用于控制只进行一次play()
},
onLoad: function () {
this.setData({
liveSrc:“rtmp://live.hkstv.hk.lxdns.com/live/hks”
})
},
onReady(res) {
this.ctx = wx.createLivePlayerContext(‘player’)
},
/* 直播相关 start */
// 静音按钮
tapMute() {
this.bindMute();
},
// 监听状态改变
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
});
}
})
},
bindStop() {
this.ctx.stop({
success: res => {
this.setData({
// state:1 - this.data.state // 0
state: 0,
init: true
});
console.log(‘stop success’)
},
fail: res => {
console.log(‘stop fail’)
this.setData({
state: -1
});
}
})
},
bindResume() {
this.ctx.resume({
success: res => {
this.setData({
// state:1 - this.data.state // 1
state: 1
});
console.log(‘resume success’)
},
fail: res => {
console.log(‘resume fail’)
this.setData({
state: -1
});
}
})
},
bindMute() {
this.ctx.mute({
success: res => {
this.setData({
mute: !this.data.mute
});
console.log(‘mute success’)
},
fail: res => {
console.log(‘mute 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
});
}
})
},
setSrc(){
this.setData({
liveSrc: “”
});
console.log(this.data.liveSrc);
this.ctx = null;
},
onShow(){
console.log(“show~~~~~~”);
// setTimeout(() => {
// this.bindPlay();
// }, 1000);
},
onHide(){
console.log(“hide~~~~~~”);
this.setData({
liveSrc: “”
});
this.bindStop();
}
/* 直播相关 end */
})