Skip to content

Commit 9cda265

Browse files
committed
fix: 规避 Android 最小化恢复后 player 创建失败问题
reason: Android 小程序退后台, 这时远端进房,创建的player播放被拦截,绑定底层SDK失败
1 parent d06a5eb commit 9cda265

File tree

2 files changed

+50
-11
lines changed

2 files changed

+50
-11
lines changed

WXMini/components/trtc-room/template/custom/custom.wxml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
/>
3232
</view>
3333
</view>
34-
<view class="view-container pusher-container {{pusher.isVisible?'':'none'}}" style="left:{{pusher.xAxis}};top:{{pusher.yAxis}};width:{{pusher.width}};height:{{pusher.height}};z-index:{{pusher.zindex}};">
34+
<view class="view-container pusher-container {{pusher.isVisible?'':'none'}}" style="left:{{pusher.xAxis}};top:{{pusher.yAxis}};width:{{pusher.width}};height:{{pusher.height}};z-index:{{pusher.zIndex}};">
3535
<live-pusher
3636
class="pusher"
3737
url="{{pusher.url}}"

WXMini/components/trtc-room/trtc-room.js

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -146,18 +146,51 @@ Component({
146146
// 经历了 5001 浮窗关闭事件,小程序底层会自动退房,恢复小程序时组件需要重新进房
147147
// 重新进房
148148
this.enterRoom({ roomID: this.data.config.roomID }).then(()=>{
149-
setTimeout(()=>{
150-
this.publishLocalVideo()
151-
this.publishLocalAudio()
152-
}, 2000)
153149
// 进房后开始推送视频或音频
150+
// setTimeout(()=>{
151+
// this.publishLocalVideo()
152+
// this.publishLocalAudio()
153+
// }, 2000)
154154
})
155+
} else if (ENV.IS_ANDROID && this.status.pageLife === 'hide' && this.status.isOnHideAddStream && this.data.streamList.length > 0) {
156+
// 微信没有提供明确的最小化事件,onHide事件,不一定是最小化
157+
// 获取所有的player 清空 src 重新赋值 验证无效
158+
// 清空 visibleStreamList 重新赋值, 验证无效
159+
// 退房重新进房,有效但是成本比较高
160+
161+
// 将标记了 isOnHideAdd 的 stream 的 palyer 销毁并重新渲染
162+
const streamList = this.data.streamList
163+
let tempStreamList = []
164+
// 过滤 onHide 时新增的 stream
165+
for (let i = 0; i < streamList.length; i++) {
166+
if (streamList[i].isOnHideAdd && streamList[i].playerContext) {
167+
const stream = streamList[i]
168+
tempStreamList.push(stream)
169+
stream.playerContext = undefined
170+
streamList.splice(i, 1)
171+
}
172+
}
173+
// 设置渲染,销毁onHide 时新增的 player
174+
this._setList({
175+
streamList: streamList,
176+
}).then(() => {
177+
for (let i = 0; i < tempStreamList.length; i++) {
178+
streamList.push(tempStreamList[i])
179+
}
180+
// 设置渲染,重新创建 onHide 时新增的 player
181+
// setTimeout(()=>{
182+
this._setList({
183+
streamList: streamList,
184+
}).then(() => {
185+
for (let i = 0; i < tempStreamList.length; i++) {
186+
tempStreamList[i] = wx.createLivePlayerContext(tempStreamList[i].streamID, this)
187+
}
188+
tempStreamList = []
189+
})
190+
// }, 500)
191+
})
192+
this.status.isOnHideAddStream = false
155193
}
156-
// if (this.status.isPush) {
157-
// // 小程序hide - show 有一定概率本地黑屏或静止,远端正常,或者远端和本地同时黑屏或静止,需要手动启动预览,非必现
158-
// this.data.pusher.getPusherContext().startPreview()
159-
// this.data.pusher.getPusherContext().resume()
160-
// }
161194
this.status.pageLife = 'show'
162195
},
163196
hide: function() {
@@ -193,6 +226,7 @@ Component({
193226
isPush: false, // 推流状态
194227
isPending: false, // 挂起状态,触发5000事件标记为true,onShow后标记为false
195228
pageLife: '', // 页面生命周期 hide, show
229+
isOnHideAddStream: false, // onHide后有新增Stream
196230
}
197231
this._lastTapTime = 0 // 点击时间戳 用于判断双击事件
198232
this._beforeLastTapTime = 0 // 点击时间戳 用于判断双击事件
@@ -1035,7 +1069,7 @@ Component({
10351069
rtcConfig.userDefineRecordID = rtcConfig.userDefineRecordID || '' // 指定录制文件的recordid
10361070
rtcConfig.privateMapKey = rtcConfig.privateMapKey || '' // 字符串房间号
10371071
rtcConfig.pureAudioMode = rtcConfig.pureAudioMode || ''// 指定是否纯音频推流及录制,默认不填,值为1 或 2,其他值非法不处理
1038-
rtcConfig.recvMode = rtcConfig.recvMode || 3 // 1. 自动接收音视频 2. 仅自动接收音频 3. 仅自动接收视频 4. 音视频都不自动接收, 不能绑定player
1072+
rtcConfig.recvMode = rtcConfig.recvMode || 1 // 1. 自动接收音视频 2. 仅自动接收音频 3. 仅自动接收视频 4. 音视频都不自动接收, 不能绑定player
10391073
let roomID = ''
10401074
if (/^\d+$/.test(rtcConfig.roomID)) {
10411075
// 数字房间号
@@ -1215,6 +1249,11 @@ Component({
12151249
this.userController.on(EVENT.REMOTE_VIDEO_ADD, (event)=>{
12161250
console.log(TAG_NAME, '远端视频可用', event, event.data.stream.userID)
12171251
const stream = event.data.stream
1252+
// 如果Android onHide 时,新增的player 无法播放 记录标识位
1253+
if (this.status.pageLife === 'hide') {
1254+
this.status.isOnHideAddStream = true
1255+
stream.isOnHideAdd = true
1256+
}
12181257
this._setList({
12191258
userList: event.data.userList,
12201259
streamList: event.data.streamList,

0 commit comments

Comments
 (0)