voice.js 4.8 KB

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