import TRTC from 'trtc-sdk-v5' import { useNotification } from 'naive-ui' class meetingService { private trtc: any = null private notification = useNotification() // 监测设备 private async detectionDevice() { let status = true const micList = await TRTC.getMicrophoneList() const speakerList = await TRTC.getSpeakerList() const hasMicrophoneDevice = micList.length > 0 const hasSpeakerDevice = speakerList.length > 0 if (!hasMicrophoneDevice || !hasSpeakerDevice) { status = false } if (hasMicrophoneDevice) { try { await this.trtc.startLocalAudio({ publish: false }) await this.trtc.enableAudioVolumeEvaluation(250) await this.trtc.stopLocalAudio() } catch (error) { status = false } } return status } // 加入房间+发布local video|audio public async joinRoom(options: { userId: string, roomId: number }, videoDomID: string) { if (!this.trtc) return await this.trtc.enterRoom(options) // 推送音视频 await this.trtc.startLocalVideo({ view: document.getElementById(videoDomID) // 在 DOM 中的 elementId 为 localVideo 的标签上预览视频。 }) // 采集默认麦克风并发布 await this.trtc.startLocalAudio() } // 退出房间 public async exitRoom() { if (!this.trtc) return await this.trtc.exitRoom() await this.trtc.stopLocalVideo() await this.trtc.stopLocalAudio() this.destroy() } // 是否静音远程用户 public async stopRomoteAudio(userId:string, mute:boolean) { if (!this.trtc) return await this.trtc.muteRemoteAudio(userId, mute) } // 关闭本地视频推送 public async stopLocalVideo() { if (!this.trtc) return await this.trtc.stopLocalVideo() } // 初始化 public async _init() { if (this.trtc) this.destroy() // creta this.trtc = TRTC.create() // 检测设备 const status = await this.detectionDevice() if (!status) return this.notification.error({ content: '请检查你的麦克风或扬声器是否正常' }) // 加入房间 // 监听房间事件 this.trtc.on(TRTC.EVENT.ERROR, this.handleError.bind(this)) // this.trtc.on(TRTC.EVENT.KICKED_OUT, this.handleKickedOut.bind(this)) this.trtc.on(TRTC.EVENT.REMOTE_USER_ENTER, this.handleRemoteUserEnter.bind(this)) this.trtc.on(TRTC.EVENT.REMOTE_USER_EXIT, this.handleRemoteUserExit.bind(this)) this.trtc.on(TRTC.EVENT.REMOTE_AUDIO_UNAVAILABLE, this.handleRemoteAudioUnavailable.bind(this)) this.trtc.on(TRTC.EVENT.REMOTE_AUDIO_AVAILABLE, this.handleRemoteAudioAvailable.bind(this)) this.trtc.on(TRTC.EVENT.REMOTE_VIDEO_AVAILABLE, this.handleRemoteVideoAvailable.bind(this)) this.trtc.on(TRTC.EVENT.REMOTE_VIDEO_UNAVAILABLE, this.handleRemoteVideoUnavailable.bind(this)) // this.trtc.on(TRTC.EVENT.AUDIO_VOLUME, this.handleAudioVolume.bind(this)) return true } // 错误信息 private handleError(error: { message: string }) { this.notification.error({ content: error.message, duration: 2000 }) } // 远程用户加入 private handleRemoteUserEnter(event: any) { console.log('远程用户加入', event) } // 远程用户离开 private handleRemoteUserExit(event: any) { console.log('远程用户离开', event) } // 远程用户音频不可用 private handleRemoteAudioUnavailable(event: any) { console.log('远程用户音频不可用', event) } // 远程用户音频可用 private handleRemoteAudioAvailable(event: any) { console.log('远程用户音频可用', event) } // 远程用户视频不可用 private handleRemoteVideoUnavailable(event: any) { console.log('远程用户视频不可用', event) } // 远程用户视频可用 private handleRemoteVideoAvailable(event: any) { console.log('远程用户视频可用', event) } private destroy() { if (this.trtc) this.trtc.destroy() this.trtc = null } } export default meetingService