فهرست منبع

重构了toggleCamera函数,将RTC频道管理与摄像头操作绑定
从joinClass和startClass函数中移除了冗余的RTC频道加入逻辑
修复了ClassStatus.IN_PROGRESS拼写错误
确保所有相关状态变量正确更新

yourname 7 ماه پیش
والد
کامیت
d7ba656fed
1فایلهای تغییر یافته به همراه18 افزوده شده و 34 حذف شده
  1. 18 34
      client/mobile/pages_classroom.tsx

+ 18 - 34
client/mobile/pages_classroom.tsx

@@ -474,26 +474,9 @@ export const ClassroomPage = () => {
       listenMessageEvents();
       listenMessageEvents();
       listenRtcEvents();
       listenRtcEvents();
 
 
-      // 加入RTC频道
-      const timestamp = Math.floor(Date.now() / 1000) + 3600 * 3;
-      const token = await generateToken(RTC_APP_ID, RTC_APP_KEY, classId, userId, timestamp);
-      aliRtcEngine.current.setChannelProfile(AliRtcSdkChannelProfile.AliRtcSdkCommunication);
-      await aliRtcEngine.current.joinChannel(
-        {
-          channelId: classId,
-          userId,
-          appId: RTC_APP_ID,
-          token,
-          timestamp,
-        },
-        userId
-      );
-
-      // 设置本地预览
+      // 设置本地预览配置
       aliRtcEngine.current.setLocalViewConfig('localPreviewer', AliRtcVideoTrack.AliRtcVideoTrackCamera);
       aliRtcEngine.current.setLocalViewConfig('localPreviewer', AliRtcVideoTrack.AliRtcVideoTrackCamera);
       
       
-      // 保留基础RTC连接,视频发布逻辑已移至startClass
-      
       setIsJoinedClass(true);
       setIsJoinedClass(true);
       setErrorMessage('');
       setErrorMessage('');
       showToast('success', '加入课堂成功');
       showToast('success', '加入课堂成功');
@@ -544,21 +527,6 @@ export const ClassroomPage = () => {
         throw new Error('RTC连接未建立');
         throw new Error('RTC连接未建立');
       }
       }
 
 
-      // 加入RTC频道
-      const timestamp = Math.floor(Date.now() / 1000) + 3600 * 3;
-      const token = await generateToken(RTC_APP_ID, RTC_APP_KEY, classId, userId, timestamp);
-      aliRtcEngine.current.setChannelProfile(AliRtcSdkChannelProfile.AliRtcSdkCommunication);
-      await aliRtcEngine.current.joinChannel(
-        {
-          channelId: classId,
-          userId,
-          appId: RTC_APP_ID,
-          token,
-          timestamp,
-        },
-        userId
-      );
-
       // 开启老师视频
       // 开启老师视频
       try {
       try {
         aliRtcEngine.current!.setLocalViewConfig('localPreviewer', AliRtcVideoTrack.AliRtcVideoTrackCamera);
         aliRtcEngine.current!.setLocalViewConfig('localPreviewer', AliRtcVideoTrack.AliRtcVideoTrackCamera);
@@ -701,12 +669,28 @@ export const ClassroomPage = () => {
     
     
     try {
     try {
       if (isCameraOn) {
       if (isCameraOn) {
+        // 关闭摄像头并退出RTC频道
         await aliRtcEngine.current.stopPreview();
         await aliRtcEngine.current.stopPreview();
+        await aliRtcEngine.current.leaveChannel();
+        showToast('info', '摄像头已关闭并退出RTC频道');
       } else {
       } else {
+        // 加入RTC频道并开启摄像头
+        const timestamp = Math.floor(Date.now() / 1000) + 3600 * 3;
+        const token = await generateToken(RTC_APP_ID, RTC_APP_KEY, classId, userId, timestamp);
+        await aliRtcEngine.current.joinChannel(
+          {
+            channelId: classId,
+            userId,
+            appId: RTC_APP_ID,
+            token,
+            timestamp,
+          },
+          userId
+        );
         await aliRtcEngine.current.startPreview();
         await aliRtcEngine.current.startPreview();
+        showToast('info', '已加入RTC频道并开启摄像头');
       }
       }
       setIsCameraOn(!isCameraOn);
       setIsCameraOn(!isCameraOn);
-      showToast('info', `摄像头已${isCameraOn ? '关闭' : '开启'}`);
     } catch (err) {
     } catch (err) {
       console.error('切换摄像头状态失败:', err);
       console.error('切换摄像头状态失败:', err);
       showToast('error', '切换摄像头失败');
       showToast('error', '切换摄像头失败');