Explorar el Código

fix 修复直播退出声音不关闭,切回应用直播画面卡住的问题

tiedan hace 4 años
padre
commit
ec98beff0d
Se han modificado 2 ficheros con 104 adiciones y 43 borrados
  1. 29 28
      components/trtc-room/trtc-room.js
  2. 75 15
      pages/zbDetails/zbDetails.js

+ 29 - 28
components/trtc-room/trtc-room.js

@@ -177,30 +177,32 @@ Component({
       console.log(TAG_NAME, 'error', error)
     },
   },
+  // 经历了 5000 挂起事件
   pageLifetimes: {
     show: function () {
       // 组件所在的页面被展示时执行
       console.log(TAG_NAME, 'show status:', this.status)
       if (this.status.isPending) {
-        // 经历了 5000 挂起事件
-        this.status.isPending = false
-        // 修复iOS 最小化触发5000事件后,音频推流失败的问题
-        // if (ENV.IS_IOS && this.data.pusher.enableMic) {
-        //   this.unpublishLocalAudio().then(()=>{
-        //     this.publishLocalAudio()
-        //   })
-        // }
-        // 经历了 5001 浮窗关闭事件,小程序底层会自动退房,恢复小程序时组件需要重新进房
-        // 重新进房
-        this.enterRoom({
-          roomID: this.data.config.roomID
-        }).then(() => {
-          // 进房后开始推送视频或音频
-          // setTimeout(()=>{
-          //   this.publishLocalVideo()
-          //   this.publishLocalAudio()
-          // }, 2000)
-        })
+        this.triggerEvent('myevent', {},  { bubbles: true, composed: true })
+        // // 经历了 5000 挂起事件
+        // this.status.isPending = false
+        // // 修复iOS 最小化触发5000事件后,音频推流失败的问题
+        // // if (ENV.IS_IOS && this.data.pusher.enableMic) {
+        // //   this.unpublishLocalAudio().then(()=>{
+        // //     this.publishLocalAudio()
+        // //   })
+        // // }
+        // // 经历了 5001 浮窗关闭事件,小程序底层会自动退房,恢复小程序时组件需要重新进房
+        // // 重新进房
+        // this.enterRoom({
+        //   roomID: this.data.config.roomID
+        // }).then(() => {
+        //   // 进房后开始推送视频或音频
+        //   // setTimeout(()=>{
+        //   //   this.publishLocalVideo()
+        //   //   this.publishLocalAudio()
+        //   // }, 2000)
+        // })
       } else if (ENV.IS_ANDROID && this.status.pageLife === 'hide' && this.status.isOnHideAddStream && this.data.streamList.length > 0) {
         // 微信没有提供明确的最小化事件,onHide事件,不一定是最小化
         // 获取所有的player 清空 src 重新赋值 验证无效
@@ -241,6 +243,7 @@ Component({
       }
       this.status.pageLife = 'show'
     },
+   
     hide: function () {
       // 组件所在的页面被隐藏时执行
       console.log(TAG_NAME, 'hide')
@@ -1507,7 +1510,6 @@ Component({
     _pusherStateChangeHandler(event) {
       const code = event.detail.code
       const message = event.detail.message
-      console.log("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!",code)
       console.log(TAG_NAME, 'pusherStateChange:', code, event)
       switch (code) {
         case 0: // 未知状态码,不做处理
@@ -1546,17 +1548,16 @@ Component({
         case 1019:
           console.log(TAG_NAME, '退出房间', code)
           // 20200421 iOS 仍然没有1019事件通知退房,退房事件移动到 exitRoom 方法里,但不是后端通知的退房成功
-         //this._emitter.emit(EVENT.LOCAL_LEAVE, { userID: this.data.pusher.userID })
-          //孙喆:20201020 ios有1019事件
+          // this._emitter.emit(EVENT.LOCAL_LEAVE, { userID: this.data.pusher.userID })
           break
         case 2003:
           console.log(TAG_NAME, '渲染首帧视频', code)
           break
-        case 1020://远端用户全量列表更新
-        case 1031://远端用户进房
-        case 1032://远端用户退房
-        case 1033://远端视频状态更新
-        case 1034://远端音频状态更新
+          case 1020://远端用户全量列表更新
+          case 1031://远端用户进房
+          case 1032://远端用户退房
+          case 1033://远端视频状态更新
+          case 1034://远端音频状态更新
           // 通过 userController 处理 1020 1031 1032 1033 1034
           this.userController.userEventHandler(event)
           break
@@ -1674,7 +1675,7 @@ Component({
       this._emitter.emit(EVENT.VIDEO_FULLSCREEN_UPDATE, event)
     },
     _playerNetStatus(event) {
-     // console.log(TAG_NAME, 'player变化了', event)
+      console.log(TAG_NAME, 'player变化了', event)
       // 获取player 视频的宽高
       const stream = this.userController.getStream({
         userID: event.currentTarget.dataset.userid,

+ 75 - 15
pages/zbDetails/zbDetails.js

@@ -15,6 +15,8 @@ Page({
     currentGroup: null,
     chatContent: [],
     inputValue: '',
+    isLoad:false,
+    // streamIdList:[],
     // second: 5,
     showmodal: false,
     bottom: "", //滑到底状态 防止弹出消息不跳到最后一条
@@ -54,6 +56,9 @@ Page({
           });
         } else {
           for (let i = 0; i < imResponse.data.groupList.length; i++) {
+            console.log(imResponse, '我是imResponse')
+            console.log(imResponse.data.groupList[i].groupID, '我是循环出来的id')
+            console.log(_this.data.roomIdandcourseId, '我是带过来的id')
             if (imResponse.data.groupList[i].groupID == _this.data.roomIdandcourseId) {
               // console.log(imResponse.data.groupList[i])
               // let pushArr = this.data.currentGroup.push(imResponse.data.groupList[i])
@@ -63,8 +68,10 @@ Page({
               })
               _this.getjyList(imResponse.data.groupList[i])
             }
+            console.log(_this.data.currentGroup)
           }
           if (_this.data.currentGroup == null) {
+            console.log('我进入null')
             let promise = _this.data.tim.joinGroup({
               groupID: _this.data.roomIdandcourseId,
               type: TIM.TYPES.GRP_MEETING
@@ -393,6 +400,7 @@ Page({
     })
   },
   async onTim() {
+    console.log("onTim++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
     this.data.tim.on(TIM.EVENT.SDK_READY, this.data.onSdkReady, this);
     this.data.tim.on(TIM.EVENT.MESSAGE_RECEIVED, this.data.onSdkRecived, this);
     const sig = await getusersig.genTestUserSig(this.data.rtcConfig.userID, this.data.rtcConfig.sdkAppID, "f1ca85809aed35d82d8bb76612787e38dada5d0a6f233662777b42bdb7eb880b")
@@ -417,6 +425,9 @@ Page({
     let promise = this.data.tim.logout();
     promise.then(function (imResponse) {
       console.log(imResponse.data, " 登出成功");
+      this.setData({
+        tim: null
+      })
     }).catch(function (imError) {
       console.warn(' 登出失败:', imError);
     });
@@ -485,29 +496,33 @@ Page({
       roomID: Number(this.data.roomIdandcourseId) //课程id
       // userID: this.data.rtcConfig.userID
     })
-
-
-
   },
   // onLoad(options) {
-  // console.log(options, "99999");
-  // this.setData({
-  //   roomIdandcourseId: options.id
-  // })
+  //   console.log(options, "我onLoad了");
+  //   this.setData({
+  //     roomIdandcourseId: options.id
+  //   })
+  //   console.log(this.data.roomIdandcourseId)
   // },
+
   async onShow() {
-    // 获取当前小程序的页面栈
     let pages = getCurrentPages();
-    // 数组中索引最大的页面--当前页面
     let currentPage = pages[pages.length - 1];
-    // 打印出当前页面中的 options
     console.log(currentPage.options.id, 'wwww')
     this.setData({
       roomIdandcourseId: currentPage.options.id
     })
-
-
-
+  if(this.data.isLoad){
+    this.trtcRoomContext.subscribeRemoteAudio({
+      userID: "0"
+    }).then(() => {
+      console.log("音频订阅")
+    })
+  }else{
+    this.setData({
+      isLoad:true
+    })
+    console.log("我onshow了")
     const sessionKey = await tools.checkSessionAndLogin();
     const information = await tools.request('/wx/student/selStudentSessionKeyEcho', {
       sessionKey
@@ -520,7 +535,6 @@ Page({
     this.setData({
       ['rtcConfig.userSig']: sig.userSig
     })
-    //保持页面常亮
     wx.setKeepScreenOn({
       keepScreenOn: true,
     })
@@ -537,12 +551,58 @@ Page({
       'cos-wx-sdk': COS
     });
     this.onTim();
+  }
+  
+    // console.log("我onshow了")
+    // const sessionKey = await tools.checkSessionAndLogin();
+    // const information = await tools.request('/wx/student/selStudentSessionKeyEcho', {
+    //   sessionKey
+    // }, 'POST');
+    // this.setData({
+    //   miniName: information.data.data.miniName,
+    //   ['rtcConfig.userID']: information.data.data.id.toString()
+    // })
+    // const sig = await getusersig.genTestUserSig(this.data.rtcConfig.userID, this.data.rtcConfig.sdkAppID, "f1ca85809aed35d82d8bb76612787e38dada5d0a6f233662777b42bdb7eb880b")
+    // this.setData({
+    //   ['rtcConfig.userSig']: sig.userSig
+    // })
+    // wx.setKeepScreenOn({
+    //   keepScreenOn: true,
+    // })
+    // this.trtcRoomContext = this.selectComponent('#trtcroom')
+    // this.bindTRTCRoomEvent()
+    // let option = {
+    //   SDKAppID: 1400404379 // 接入时需要将0替换为您的即时通信 IM 应用的 SDKAppID
+    // };
+    // this.setData({
+    //   tim: TIM.create(option)
+    // })
+    // this.data.tim.setLogLevel(0); // 普通级别,日志量较多,接入时建议使用
+    // this.data.tim.registerPlugin({
+    //   'cos-wx-sdk': COS
+    // });
+    // this.onTim();
   },
   onHide() {
     console.log("zbdetail-onhide了")
     this.trtcRoomContext.unpublishLocalVideo();
     this.trtcRoomContext.unpublishLocalAudio();
-    this.offTim();
+    // this.offTim();
+    // this.trtcRoomContext.unsubscribeRemoteVideo({
+    //   userID: "0",
+    //   streamType: "main"
+    // }).then(() => {
+    //   console.log("远端视频订阅成功")
+    // })
+    this.trtcRoomContext.unsubscribeRemoteAudio({
+      userID: "0"
+    }).then(() => {
+      console.log("音频取消订阅")
+    })
+    // this.trtcRoomContext.exitRoom().then(()=>{
+    //   console.log("退出房间成功")
+    //   // 退出房间成功
+    // })
   },
   onUnload() {
     console.log("zbdetail-onUnload")