voice.js 4.7 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.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. this.setData({show: true})
  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({show: false})
  67. },
  68. },
  69. /**
  70. * 组件的方法列表
  71. */
  72. methods: {
  73. enterAudioRoom(user, noEnter) {
  74. // if (this.hasEnter) {
  75. // return
  76. // }
  77. console.error('hasEnter', this.hasEnter)
  78. this.hasEnter = true
  79. getApp().globalData.audioUser = user
  80. console.log(user, 'user')
  81. this.setData({
  82. trtcConfig: {
  83. scene: 'rtc',
  84. sdkAppID: '1400450635', // 开通实时音视频服务创建应用后分配的 SDKAppID
  85. userID: user.userId, // 用户 ID,可以由您的帐号系统指定
  86. userSig: user.sig, // 身份签名,相当于登录密码的作用
  87. template: 'custom', // 画面排版模式
  88. }
  89. }, () => {
  90. this.trtcRoomContext = this.selectComponent('#trtcroom')
  91. if (noEnter) {
  92. return
  93. }
  94. console.log(this.trtcRoomContext, )
  95. let EVENT =this.trtcRoomContext.EVENT
  96. if (this.trtcRoomContext) {
  97. this.trtcRoomContext.on(EVENT.LOCAL_JOIN, (event) => {
  98. // 进房成功后发布本地音频流和视频流
  99. console.error('进入房间')
  100. this.successEnter = true
  101. if(user.role=='leader'){
  102. if (user.noMute) {
  103. this.unpublishLocalAudio()
  104. } else {
  105. this.publishLocalAudio()
  106. }
  107. }
  108. })
  109. this.trtcRoomContext.on(EVENT.LOCAL_LEAVE, (event) => {
  110. console.error('离开房间')
  111. })
  112. // 监听远端用户的音频流的变更事件
  113. this.trtcRoomContext.on(EVENT.REMOTE_AUDIO_ADD, (event) => {
  114. // 订阅(即播放)远端用户的音频流
  115. let userID = event.data.userID
  116. this.trtcRoomContext.subscribeRemoteAudio({
  117. userID: userID
  118. })
  119. })
  120. // this.trtcRoomContext.on(EVENT.LOCAL_AUDIO_VOLUME_UPDATE, event => {
  121. // console.log(event)
  122. // })
  123. this.trtcRoomContext.enterRoom({
  124. roomID: getApp().globalData.roomId
  125. }).then(() => {
  126. console.log('成功进入房间')
  127. }).catch((res) => {
  128. console.error('room joinRoom 进房失败:', res)
  129. this.hasEnter = false
  130. })
  131. let timer = null
  132. timer = setInterval(() => {
  133. if (!this.successEnter) {
  134. this.trtcRoomContext.enterRoom({
  135. roomID: getApp().globalData.roomId
  136. }).then(() => {
  137. console.log('成功进入房间')
  138. }).catch((res) => {
  139. console.error('room joinRoom 进房失败:', res)
  140. this.hasEnter = false
  141. })
  142. } else {
  143. clearInterval(timer)
  144. timer = null
  145. }
  146. }, 3000);
  147. }
  148. })
  149. },
  150. unpublishLocalAudio () {
  151. this.trtcRoomContext && this.trtcRoomContext.unpublishLocalAudio()
  152. },
  153. publishLocalAudio () {
  154. this.trtcRoomContext && this.trtcRoomContext.publishLocalAudio()
  155. }
  156. }
  157. })