voice.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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.role=='leader'){
  33. if (user.noMute) {
  34. this.unpublishLocalAudio()
  35. } else {
  36. this.publishLocalAudio()
  37. // }
  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')
  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. // wx.showToast({
  56. // title: 'show',
  57. // })
  58. this.setData({show: true})
  59. // this.trtcRoomContext = this.selectComponent('#trtcroom')
  60. // if (getApp().globalData.audioUser) {
  61. // this.enterAudioRoom(getApp().globalData.audioUser, true)
  62. // }
  63. // if (this.data.show) {
  64. // this.enterAudioRoom()
  65. // }
  66. },
  67. hide() {
  68. clearTimeout(this.time)
  69. this.setData({show: false})
  70. },
  71. },
  72. /**
  73. * 组件的方法列表
  74. */
  75. methods: {
  76. enterAudioRoom(user, noEnter) {
  77. // if (this.hasEnter) {
  78. // return
  79. // }
  80. console.error('hasEnter', this.hasEnter)
  81. this.hasEnter = true
  82. getApp().globalData.audioUser = user
  83. console.log(user, 'user')
  84. this.setData({
  85. trtcConfig: {
  86. scene: 'rtc',
  87. sdkAppID: '1400450635', // 开通实时音视频服务创建应用后分配的 SDKAppID
  88. userID: user.userId, // 用户 ID,可以由您的帐号系统指定
  89. userSig: user.sig, // 身份签名,相当于登录密码的作用
  90. template: 'custom', // 画面排版模式
  91. }
  92. }, () => {
  93. this.trtcRoomContext = this.selectComponent('#trtcroom')
  94. if (noEnter) {
  95. return
  96. }
  97. console.log(this.trtcRoomContext, )
  98. let EVENT =this.trtcRoomContext.EVENT
  99. if (this.trtcRoomContext) {
  100. this.trtcRoomContext.on(EVENT.LOCAL_JOIN, (event) => {
  101. // 进房成功后发布本地音频流和视频流
  102. console.error('进入房间')
  103. this.successEnter = true
  104. if(user.role=='leader'){
  105. if (user.noMute) {
  106. this.unpublishLocalAudio()
  107. } else {
  108. this.publishLocalAudio()
  109. }
  110. }
  111. })
  112. this.trtcRoomContext.on(EVENT.LOCAL_LEAVE, (event) => {
  113. console.error('离开房间')
  114. })
  115. // 监听远端用户的音频流的变更事件
  116. this.trtcRoomContext.on(EVENT.REMOTE_AUDIO_ADD, (event) => {
  117. // 订阅(即播放)远端用户的音频流
  118. let userID = event.data.userID
  119. this.trtcRoomContext.subscribeRemoteAudio({
  120. userID: userID
  121. })
  122. })
  123. // this.trtcRoomContext.on(EVENT.LOCAL_AUDIO_VOLUME_UPDATE, event => {
  124. // console.log(event)
  125. // })
  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. })