voice.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // component/voice/voice.js
  2. Component({
  3. /**
  4. * 组件的属性列表
  5. */
  6. properties: {
  7. },
  8. /**
  9. * 组件的初始数据
  10. */
  11. data: {
  12. show: true,
  13. pushUrl: false,
  14. pullUrls: [],
  15. trtcConfig: {},
  16. room: 0
  17. },
  18. attached() {
  19. this.callback = (user = {}) => {
  20. console.warn('---user---', user, '---user---')
  21. if (user.action === 'stopCall' && this.hasEnter) {
  22. this.unpublishLocalAudio()
  23. this.trtcRoomContext.exitRoom()
  24. this.hasEnter = false
  25. return
  26. }
  27. if (user.userId) {
  28. this.enterAudioRoom(user)
  29. return
  30. }
  31. console.warn(user.noMute, '---noMute')
  32. if (user.noMute) {
  33. this.unpublishLocalAudio()
  34. } else {
  35. this.publishLocalAudio()
  36. }
  37. }
  38. // console.log(getApp().globalData.pusher, 'getApp().globalData.pusher')
  39. // if (getApp().globalData.pusher) {
  40. // this.callback(getApp().globalData.pusher)
  41. // }
  42. console.log('attch')
  43. getApp().addVoicePropsListener(this.callback)
  44. },
  45. detached() {
  46. console.log('detached')
  47. getApp().removeVoicePropsListener(this.callback)
  48. // this.trtcRoomContext.exitRoom()
  49. this.hasEnter = false
  50. },
  51. pageLifetimes: {
  52. show() {
  53. this.setData({
  54. show: true
  55. })
  56. // this.trtcRoomContext = this.selectComponent('#trtcroom')
  57. // if (getApp().globalData.audioUser) {
  58. // this.enterAudioRoom(getApp().globalData.audioUser, true)
  59. // }
  60. // if (this.data.show) {
  61. // this.enterAudioRoom()
  62. // }
  63. },
  64. hide() {
  65. clearTimeout(this.time)
  66. this.setData({
  67. show: false
  68. })
  69. },
  70. },
  71. /**
  72. * 组件的方法列表
  73. */
  74. methods: {
  75. enterAudioRoom(user, noEnter) {
  76. // if (this.hasEnter) {
  77. // return
  78. // }
  79. console.error('hasEnter', this.hasEnter)
  80. this.hasEnter = true
  81. getApp().globalData.audioUser = user
  82. console.log(user, 'user')
  83. this.setData({
  84. trtcConfig: {
  85. scene: 'rtc',
  86. sdkAppID: '1400450635', // 开通实时音视频服务创建应用后分配的 SDKAppID
  87. userID: user.userId, // 用户 ID,可以由您的帐号系统指定
  88. userSig: user.sig, // 身份签名,相当于登录密码的作用
  89. template: 'custom', // 画面排版模式
  90. }
  91. }, () => {
  92. this.trtcRoomContext = this.selectComponent('#trtcroom')
  93. if (noEnter) {
  94. return
  95. }
  96. console.log(this.trtcRoomContext, )
  97. let EVENT = this.trtcRoomContext.EVENT
  98. if (this.trtcRoomContext) {
  99. this.trtcRoomContext.on(EVENT.LOCAL_JOIN, (event) => {
  100. // 进房成功后发布本地音频流和视频流
  101. console.error('进入房间')
  102. this.successEnter = true
  103. if (user.role == 'leader') {
  104. if (user.noMute) {
  105. this.unpublishLocalAudio()
  106. } else {
  107. this.publishLocalAudio()
  108. }
  109. }
  110. })
  111. this.trtcRoomContext.on(EVENT.LOCAL_LEAVE, (event) => {
  112. console.error('离开房间')
  113. })
  114. // 监听远端用户的音频流的变更事件
  115. this.trtcRoomContext.on(EVENT.REMOTE_AUDIO_ADD, (event) => {
  116. // 订阅(即播放)远端用户的音频流
  117. let userID = event.data.userID
  118. this.trtcRoomContext.subscribeRemoteAudio({
  119. userID: userID
  120. })
  121. })
  122. // this.trtcRoomContext.on(EVENT.LOCAL_AUDIO_VOLUME_UPDATE, event => {
  123. // console.log(event)
  124. // })
  125. this.trtcRoomContext.enterRoom({
  126. roomID: getApp().globalData.roomId
  127. }).then(() => {
  128. console.log('成功进入房间')
  129. }).catch((res) => {
  130. console.error('room joinRoom 进房失败:', res)
  131. this.hasEnter = false
  132. })
  133. let timer = null
  134. timer = setInterval(() => {
  135. if (!this.successEnter) {
  136. this.trtcRoomContext.enterRoom({
  137. roomID: getApp().globalData.roomId
  138. }).then(() => {
  139. console.log('成功进入房间')
  140. }).catch((res) => {
  141. console.error('room joinRoom 进房失败:', res)
  142. this.hasEnter = false
  143. })
  144. } else {
  145. clearInterval(timer)
  146. timer = null
  147. }
  148. }, 3000);
  149. }
  150. })
  151. },
  152. unpublishLocalAudio() {
  153. this.trtcRoomContext && this.trtcRoomContext.unpublishLocalAudio()
  154. },
  155. publishLocalAudio() {
  156. this.trtcRoomContext && this.trtcRoomContext.publishLocalAudio()
  157. }
  158. }
  159. })