Просмотр исходного кода

将 joinChannel 功能提取到单独函数 joinRtcChannel
将 leaveChannel 功能提取到单独函数 leaveRtcChannel
重构 toggleCamera 只处理摄像头开关逻辑

yourname 7 месяцев назад
Родитель
Сommit
af3d15161c
1 измененных файлов с 43 добавлено и 23 удалено
  1. 43 23
      client/mobile/pages_classroom.tsx

+ 43 - 23
client/mobile/pages_classroom.tsx

@@ -645,34 +645,54 @@ export const ClassroomPage = () => {
     }
   };
 
-  // 切换摄像头状态
-  const toggleCamera = async () => {
+  // 加入RTC频道
+  const joinRtcChannel = async () => {
     if (!aliRtcEngine.current) return;
     
+    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
+    );
+    showToast('info', '已加入RTC频道');
+  };
+
+  // 离开RTC频道
+  const leaveRtcChannel = async () => {
+    if (!aliRtcEngine.current) return;
+    await aliRtcEngine.current.leaveChannel();
+    showToast('info', '已离开RTC频道');
+  };
+
+  // 开启摄像头预览
+  const startCameraPreview = async () => {
+    if (!aliRtcEngine.current) return;
+    aliRtcEngine.current.setLocalViewConfig('localPreviewer', AliRtcVideoTrack.AliRtcVideoTrackCamera);
+    await aliRtcEngine.current.startPreview();
+    showToast('info', '摄像头已开启');
+  };
+
+  // 关闭摄像头预览
+  const stopCameraPreview = async () => {
+    if (!aliRtcEngine.current) return;
+    await aliRtcEngine.current.stopPreview();
+    showToast('info', '摄像头已关闭');
+  };
+
+  // 切换摄像头状态
+  const toggleCamera = async () => {
     try {
       if (isCameraOn) {
-        // 关闭摄像头并退出RTC频道
-        await aliRtcEngine.current.stopPreview();
-        await aliRtcEngine.current.leaveChannel();
-        showToast('info', '摄像头已关闭并退出RTC频道');
+        await stopCameraPreview();
       } 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
-        );
-        // 统一设置本地预览配置
-        aliRtcEngine.current.setLocalViewConfig('localPreviewer', AliRtcVideoTrack.AliRtcVideoTrackCamera);
-        await aliRtcEngine.current.startPreview();
-        showToast('info', '已加入RTC频道并开启摄像头');
+        await startCameraPreview();
       }
       setIsCameraOn(!isCameraOn);
     } catch (err) {