Device.vue 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <template>
  2. <div></div>
  3. </template>
  4. <script setup>
  5. import { defineEmits, computed, ref } from "vue";
  6. import TRTC from "trtc-js-sdk";
  7. import { useStore } from "vuex";
  8. import { Dialog } from "@/global_components/";
  9. import browser from "@/utils/browser";
  10. const store = useStore();
  11. const videoDeviceId = computed(() => store.getters["rtc/videoDeviceId"]);
  12. const audioDeviceId = computed(() => store.getters["rtc/audioDeviceId"]);
  13. const role = ref(browser.getURLParam("role"));
  14. const emit = defineEmits(["switchDevice", "canUseDevice"]);
  15. const updateDevice = async () => {
  16. console.log("updateDevice");
  17. const cameraItems = await TRTC.getCameras();
  18. cameraItems.forEach((item) => {
  19. item.value = item.deviceId;
  20. });
  21. const microphoneItems = await TRTC.getMicrophones();
  22. microphoneItems.forEach((item) => {
  23. item.value = item.deviceId;
  24. });
  25. store.commit("rtc/setDeviceList", {
  26. cameraList: cameraItems,
  27. microphoneList: microphoneItems,
  28. });
  29. if (!videoDeviceId.value) {
  30. if (cameraItems[0]) {
  31. store.commit("rtc/setVideoDeviceId", cameraItems[0].deviceId);
  32. } else {
  33. Dialog.toast({ content: `無法獲取您的攝像頭權限`, type: "error" });
  34. }
  35. }
  36. if (!audioDeviceId.value) {
  37. if (microphoneItems[0]) {
  38. store.commit("rtc/setAudioDeviceId", microphoneItems[0].deviceId);
  39. } else {
  40. Dialog.toast({ content: `無法獲取您的麥克風權限`, type: "error" });
  41. }
  42. }
  43. };
  44. let quxian = { audio: true };
  45. if (role.value == "leader") {
  46. quxian["video"] == true;
  47. }
  48. navigator.mediaDevices
  49. .getUserMedia(quxian)
  50. .then((stream) => {
  51. stream.getTracks().forEach((track) => {
  52. track.stop();
  53. });
  54. updateDevice();
  55. emit("canUseDevice", true);
  56. })
  57. .catch((error) => {
  58. console.log(error, "error");
  59. Dialog.toast({ content: `請授權您的麥克風${role.value == "leader" ? "和攝像頭" : ""}權限`, type: "error" });
  60. });
  61. navigator.mediaDevices.ondevicechange = updateDevice;
  62. const handleDeviceChange = () => {
  63. emit("switchDevice", {
  64. videoId: store.videoDeviceId,
  65. audioId: store.audioDeviceId,
  66. });
  67. };
  68. </script>