voice.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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', getApp())
  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. userID: user.userId, // 用户 ID,可以由您的帐号系统指定
  87. // sdkAppID: '1400431163', // 开通实时音视频服务创建应用后分配的 SDKAppID
  88. // userSig: user.sig, // 身份签名,相当于登录密码的作用
  89. sdkAppID: "1400685498",
  90. userSig: 'eJwtzcsOgjAQBdB-6RaDU2jLI3EhsrHRBdGNK2Po0IyvNAWJxvjvEmA5597c*bLj7hD26FnOohDYYrzJ4LOjhkZ*tejPd7wY9EJwnsV8brXmdnGODMu5AEggExBNSUcPHFQpnkopIJkU34784ApECjBvkB1e8MLJfWyLOAis06Wut4b0tVdL77RVTb35dGXby6o6rVfs9wdhLDRy',
  91. template: 'custom', // 画面排版模式
  92. }
  93. }, () => {
  94. this.trtcRoomContext = this.selectComponent('#trtcroom')
  95. if (noEnter) {
  96. return
  97. }
  98. console.log(this.trtcRoomContext, )
  99. let EVENT = this.trtcRoomContext.EVENT
  100. if (this.trtcRoomContext) {
  101. this.trtcRoomContext.on(EVENT.LOCAL_JOIN, (event) => {
  102. // 进房成功后发布本地音频流和视频流
  103. console.error('进入房间')
  104. this.successEnter = true
  105. if (user.noMute) {
  106. this.unpublishLocalAudio()
  107. } else {
  108. this.publishLocalAudio()
  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. console.error('join')
  126. this.trtcRoomContext.enterRoom({
  127. roomID: getApp().globalData.roomId
  128. }).then(() => {
  129. console.log('成功进入房间')
  130. }).catch((res) => {
  131. console.error('room joinRoom 进房失败:', res)
  132. this.hasEnter = false
  133. })
  134. let timer = null
  135. timer = setInterval(() => {
  136. if (!this.successEnter) {
  137. this.trtcRoomContext.enterRoom({
  138. roomID: getApp().globalData.roomId
  139. }).then(() => {
  140. console.log('成功进入房间')
  141. }).catch((res) => {
  142. console.error('room joinRoom 进房失败:', res)
  143. this.hasEnter = false
  144. })
  145. } else {
  146. clearInterval(timer)
  147. timer = null
  148. }
  149. }, 3000);
  150. }
  151. })
  152. },
  153. unpublishLocalAudio() {
  154. this.trtcRoomContext && this.trtcRoomContext.unpublishLocalAudio()
  155. },
  156. publishLocalAudio() {
  157. this.trtcRoomContext && this.trtcRoomContext.publishLocalAudio()
  158. }
  159. }
  160. })