tremble 3 years ago
parent
commit
3636354e4d
2 changed files with 20 additions and 17 deletions
  1. 9 12
      src/components/RTC/Trtccom.vue
  2. 11 5
      src/components/RTC/trtc/Device.vue

+ 9 - 12
src/components/RTC/Trtccom.vue

@@ -70,12 +70,11 @@ watch(
   }
 );
 
-
 watch(
   () => isJoined.value,
   () => {
     if (!isJoined.value) {
-      handleLeave()
+      handleLeave();
     }
   }
 );
@@ -95,19 +94,12 @@ async function createLocalStream(video = true) {
       userId: store.getters["rtc/userId"],
       audio: true,
       video: video,
-      cameraId: store.getters["rtc/videoDeviceId"],
+      cameraId: video ? store.getters["rtc/videoDeviceId"] : null,
       microphoneId: store.getters["rtc/audioDeviceId"],
     });
     localStream.setVideoProfile("480p");
 
     await localStream.initialize();
-
-    localStream
-      .play("local")
-      .then(() => {
-        // addLocalControlView();
-      })
-      .catch((e) => {});
   } catch (error) {}
 }
 
@@ -135,7 +127,12 @@ async function handleJoin() {
 
   await createLocalStream();
   await handlePublish();
-
+  localStream
+    .play("local")
+    .then(() => {
+      // addLocalControlView();
+    })
+    .catch((e) => {});
   localClient.on("stream-added", (event) => {
     const remoteStream = event.stream;
     // //订阅远端流
@@ -330,7 +327,7 @@ async function handleStreamSubscribed(event) {
     remoteStream
       .play(clientId.value)
       .then(() => {
-        console.log(`RemoteStream play success`,88888888888888888888);
+        console.log(`RemoteStream play success`, 88888888888888888888);
       })
       .catch((error) => {
         console.log(`RemoteStream play failed:  error: ${error.message_}`);

+ 11 - 5
src/components/RTC/trtc/Device.vue

@@ -3,7 +3,7 @@
 </template>
 
 <script setup>
-import { defineEmits, computed,ref } from "vue";
+import { defineEmits, computed, ref } from "vue";
 import TRTC from "trtc-js-sdk";
 import { useStore } from "vuex";
 import { Dialog } from "@/global_components/";
@@ -15,7 +15,7 @@ const videoDeviceId = computed(() => store.getters["rtc/videoDeviceId"]);
 const audioDeviceId = computed(() => store.getters["rtc/audioDeviceId"]);
 const role = ref(browser.getURLParam("role"));
 
-const emit = defineEmits(["switchDevice","canUseDevice"]);
+const emit = defineEmits(["switchDevice", "canUseDevice"]);
 
 const updateDevice = async () => {
   console.log("updateDevice");
@@ -42,8 +42,14 @@ const updateDevice = async () => {
   }
 };
 
+let quxian = { audio: true };
+
+if (role.value == "leader") {
+  quxian["video"] == true;
+}
+
 navigator.mediaDevices
-  .getUserMedia({ audio: true, video: role.value == "customer" })
+  .getUserMedia(quxian)
   .then((stream) => {
     stream.getTracks().forEach((track) => {
       track.stop();
@@ -52,8 +58,8 @@ navigator.mediaDevices
     emit("canUseDevice", true);
   })
   .catch((error) => {
-    console.log(error,'error');
-    Dialog.toast({ content: `请授权您的麦克风${role.value == "leader"?'和摄像头':''}权限`, type: "error" });
+    console.log(error, "error");
+    Dialog.toast({ content: `请授权您的麦克风${role.value == "leader" ? "和摄像头" : ""}权限`, type: "error" });
   });
 
 navigator.mediaDevices.ondevicechange = updateDevice;