feige
2024-12-07 c911a53f1e5bb45e4d32ca5e912a0ad4f53c9bfa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<!--
 * @Description: 设备选择组件
 * @Date: 2022-03-10 15:47:24
 * @LastEditTime: 2022-03-29 17:02:21
-->
<template>
  <div class="select-container">
    <span class="label">{{ deviceType=='camera'? '摄像头':'麦克风' }}</span>
    <el-select
      class="select"
      v-model="activeDeviceId"
      :placeholder="deviceType"
      @change="handleChange">
      <el-option
        v-for="item in deviceList"
        :key="item.deviceId"
        :label="item.label"
        :value="item.deviceId">
      </el-option>
    </el-select>
  </div>
</template>
 
<script>
import TRTC from 'trtc-js-sdk';
export default {
  name: 'compDeviceSelect',
  props: {
    deviceType: {
      type: String,
    },
  },
  data() {
    return {
      deviceList: [],
      activeDevice: {},
      activeDeviceId: '',
    };
  },
  methods: {
    async getDeviceList() {
      switch (this.deviceType) {
        case 'camera':
          this.deviceList = await TRTC.getCameras();
          break;
        case 'microphone':
          this.deviceList = await TRTC.getMicrophones();
          break;
        case 'speaker':
          this.deviceList = await TRTC.getSpeakers();
          break;
        default:
          break;
      }
      [this.activeDevice] = this.deviceList;
      this.activeDeviceId = this.deviceList[0].deviceId;
      this.$emit('change', this.activeDeviceId);
    },
    handleChange() {
      this.$emit('change', this.activeDeviceId);
    },
  },
  mounted() {
    navigator.mediaDevices.getUserMedia({ audio: true, video: true }).then(() => {
      this.getDeviceList();
    });
    navigator.mediaDevices.addEventListener('devicechange', this.getDeviceList);
  },
  beforeDestroy() {
    navigator.mediaDevices.removeEventListener('devicechange', this.getDeviceList);
  },
};
</script>
 
<style lang="scss" scoped>
.select-container {
  display: flex;
  .label {
    display: inline-block;
    padding: 0 20px;
    width: 120px;
    height: 36px;
    text-align: left;
    line-height: 40px;
    border-top: 1px solid #DCDFE6;
    border-left: 1px solid #DCDFE6;
    border-bottom: 1px solid #DCDFE6;
    border-radius: 4px 0 0 4px;
    color: #909399;
    background-color: #F5F7FA;
    font-weight: bold;
  }
  .select {
    flex-grow: 1;
  }
}
</style>
 
<style lang="scss">
.select {
  input {
    border-radius: 0 4px 4px 0 !important;
  }
}
</style>