-
业务场景描述:适用于在微信小程序中调用视频流媒体服务加载显示直播流
-
前期设置,登录微信公众平台,调用视频只针对如下类目小程序开放
需在设置-服务类目中设置 -
开通实时播放音视频流接口
选择开发-接口设置-实时播放音视频接口 -
在绑定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
});
}
})
},
})