voice.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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: "1400685498",
  92. userSig: 'eJwtzcsOgjAQBdB-6RaDU2jLI3EhsrHRBdGNK2Po0IyvNAWJxvjvEmA5597c*bLj7hD26FnOohDYYrzJ4LOjhkZ*tejPd7wY9EJwnsV8brXmdnGODMu5AEggExBNSUcPHFQpnkopIJkU34784ApECjBvkB1e8MLJfWyLOAis06Wut4b0tVdL77RVTb35dGXby6o6rVfs9wdhLDRy',
  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('进入房间')
  106. this.successEnter = true
  107. if (user.noMute) {
  108. this.unpublishLocalAudio()
  109. } else {
  110. this.publishLocalAudio()
  111. }
  112. })
  113. this.trtcRoomContext.on(EVENT.LOCAL_LEAVE, (event) => {
  114. console.error('离开房间')
  115. })
  116. // 监听远端用户的音频流的变更事件
  117. this.trtcRoomContext.on(EVENT.REMOTE_AUDIO_ADD, (event) => {
  118. // 订阅(即播放)远端用户的音频流
  119. let userID = event.data.userID
  120. this.trtcRoomContext.subscribeRemoteAudio({
  121. userID: userID
  122. })
  123. })
  124. // this.trtcRoomContext.on(EVENT.LOCAL_AUDIO_VOLUME_UPDATE, event => {
  125. // console.log(event)
  126. // })
  127. console.error('join')
  128. this.trtcRoomContext.enterRoom({
  129. roomID: getApp().globalData.roomId
  130. }).then(() => {
  131. console.log('成功进入房间')
  132. }).catch((res) => {
  133. console.error('room joinRoom 进房失败:', res)
  134. this.hasEnter = false
  135. })
  136. // timer = setInterval(() => {
  137. // if (!this.successEnter) {
  138. // this.trtcRoomContext.enterRoom({
  139. // roomID: getApp().globalData.roomId
  140. // }).then(() => {
  141. // console.log('成功进入房间')
  142. // }).catch((res) => {
  143. // console.error('room joinRoom 进房失败:', res)
  144. // this.hasEnter = false
  145. // })
  146. // } else {
  147. // clearInterval(timer)
  148. // timer = null
  149. // }
  150. // }, 3000);
  151. }
  152. })
  153. },
  154. unpublishLocalAudio() {
  155. this.trtcRoomContext && this.trtcRoomContext.unpublishLocalAudio()
  156. },
  157. publishLocalAudio() {
  158. this.trtcRoomContext && this.trtcRoomContext.publishLocalAudio()
  159. }
  160. }
  161. })