meeting.service.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import TRTC from 'trtc-sdk-v5'
  2. import { useNotification } from 'naive-ui'
  3. class meetingService {
  4. private trtc: any = null
  5. private notification = useNotification()
  6. // 监测设备
  7. private async detectionDevice() {
  8. let status = true
  9. const micList = await TRTC.getMicrophoneList()
  10. const speakerList = await TRTC.getSpeakerList()
  11. const hasMicrophoneDevice = micList.length > 0
  12. const hasSpeakerDevice = speakerList.length > 0
  13. if (!hasMicrophoneDevice || !hasSpeakerDevice) {
  14. status = false
  15. }
  16. if (hasMicrophoneDevice) {
  17. try {
  18. await this.trtc.startLocalAudio({ publish: false })
  19. await this.trtc.enableAudioVolumeEvaluation(250)
  20. await this.trtc.stopLocalAudio()
  21. } catch (error) {
  22. status = false
  23. }
  24. }
  25. return status
  26. }
  27. // 加入房间+发布local video|audio
  28. public async joinRoom(options: { userId: string, roomId: number }, videoDomID: string) {
  29. if (!this.trtc) return
  30. await this.trtc.enterRoom(options)
  31. // 推送音视频
  32. await this.trtc.startLocalVideo({
  33. view: document.getElementById(videoDomID) // 在 DOM 中的 elementId 为 localVideo 的标签上预览视频。
  34. })
  35. // 采集默认麦克风并发布
  36. await this.trtc.startLocalAudio()
  37. }
  38. // 退出房间
  39. public async exitRoom() {
  40. if (!this.trtc) return
  41. await this.trtc.exitRoom()
  42. await this.trtc.stopLocalVideo()
  43. await this.trtc.stopLocalAudio()
  44. this.destroy()
  45. }
  46. // 是否静音远程用户
  47. public async stopRomoteAudio(userId:string, mute:boolean) {
  48. if (!this.trtc) return
  49. await this.trtc.muteRemoteAudio(userId, mute)
  50. }
  51. // 关闭本地视频推送
  52. public async stopLocalVideo() {
  53. if (!this.trtc) return
  54. await this.trtc.stopLocalVideo()
  55. }
  56. // 初始化
  57. public async _init() {
  58. if (this.trtc) this.destroy()
  59. // creta
  60. this.trtc = TRTC.create()
  61. // 检测设备
  62. const status = await this.detectionDevice()
  63. if (!status) return this.notification.error({ content: '请检查你的麦克风或扬声器是否正常' })
  64. // 加入房间
  65. // 监听房间事件
  66. this.trtc.on(TRTC.EVENT.ERROR, this.handleError.bind(this))
  67. // this.trtc.on(TRTC.EVENT.KICKED_OUT, this.handleKickedOut.bind(this))
  68. this.trtc.on(TRTC.EVENT.REMOTE_USER_ENTER, this.handleRemoteUserEnter.bind(this))
  69. this.trtc.on(TRTC.EVENT.REMOTE_USER_EXIT, this.handleRemoteUserExit.bind(this))
  70. this.trtc.on(TRTC.EVENT.REMOTE_AUDIO_UNAVAILABLE, this.handleRemoteAudioUnavailable.bind(this))
  71. this.trtc.on(TRTC.EVENT.REMOTE_AUDIO_AVAILABLE, this.handleRemoteAudioAvailable.bind(this))
  72. this.trtc.on(TRTC.EVENT.REMOTE_VIDEO_AVAILABLE, this.handleRemoteVideoAvailable.bind(this))
  73. this.trtc.on(TRTC.EVENT.REMOTE_VIDEO_UNAVAILABLE, this.handleRemoteVideoUnavailable.bind(this))
  74. // this.trtc.on(TRTC.EVENT.AUDIO_VOLUME, this.handleAudioVolume.bind(this))
  75. return true
  76. }
  77. // 错误信息
  78. private handleError(error: { message: string }) {
  79. this.notification.error({
  80. content: error.message,
  81. duration: 2000
  82. })
  83. }
  84. // 远程用户加入
  85. private handleRemoteUserEnter(event: any) {
  86. console.log('远程用户加入', event)
  87. }
  88. // 远程用户离开
  89. private handleRemoteUserExit(event: any) {
  90. console.log('远程用户离开', event)
  91. }
  92. // 远程用户音频不可用
  93. private handleRemoteAudioUnavailable(event: any) {
  94. console.log('远程用户音频不可用', event)
  95. }
  96. // 远程用户音频可用
  97. private handleRemoteAudioAvailable(event: any) {
  98. console.log('远程用户音频可用', event)
  99. }
  100. // 远程用户视频不可用
  101. private handleRemoteVideoUnavailable(event: any) {
  102. console.log('远程用户视频不可用', event)
  103. }
  104. // 远程用户视频可用
  105. private handleRemoteVideoAvailable(event: any) {
  106. console.log('远程用户视频可用', event)
  107. }
  108. private destroy() {
  109. if (this.trtc) this.trtc.destroy()
  110. this.trtc = null
  111. }
  112. }
  113. export default meetingService