voice.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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')
  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({show: true})
  54. // this.trtcRoomContext = this.selectComponent('#trtcroom')
  55. // if (getApp().globalData.audioUser) {
  56. // this.enterAudioRoom(getApp().globalData.audioUser, true)
  57. // }
  58. // if (this.data.show) {
  59. // this.enterAudioRoom()
  60. // }
  61. },
  62. hide() {
  63. clearTimeout(this.time)
  64. this.setData({show: false})
  65. },
  66. },
  67. /**
  68. * 组件的方法列表
  69. */
  70. methods: {
  71. enterAudioRoom(user, noEnter) {
  72. if (this.hasEnter) {
  73. return
  74. }
  75. console.error('重进房间')
  76. console.error('hasEnter', this.hasEnter)
  77. this.hasEnter = true
  78. getApp().globalData.audioUser = user
  79. console.log(user, 'user')
  80. this.setData({
  81. trtcConfig: {
  82. scene: 'rtc',
  83. sdkAppID: user.sdkAppID || '1400431163', // 开通实时音视频服务创建应用后分配的 SDKAppID
  84. userID: user.userId, // 用户 ID,可以由您的帐号系统指定
  85. userSig: user.sig, // 身份签名,相当于登录密码的作用
  86. template: 'custom', // 画面排版模式
  87. }
  88. }, () => {
  89. console.error(this.data.trtcConfig)
  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.ERROR, (err) => {
  98. console.error(err)
  99. })
  100. this.trtcRoomContext.on(EVENT.LOCAL_JOIN, (event) => {
  101. // 进房成功后发布本地音频流和视频流
  102. console.error('--------进入房间lalal')
  103. this.successEnter = true
  104. if (user.noMute) {
  105. this.unpublishLocalAudio()
  106. } else {
  107. this.publishLocalAudio()
  108. }
  109. })
  110. this.trtcRoomContext.on(EVENT.LOCAL_LEAVE, (event) => {
  111. console.error('离开房间')
  112. })
  113. // 监听远端用户的音频流的变更事件
  114. this.trtcRoomContext.on(EVENT.REMOTE_AUDIO_ADD, (event) => {
  115. // 订阅(即播放)远端用户的音频流
  116. let userID = event.data.userID
  117. this.trtcRoomContext.subscribeRemoteAudio({
  118. userID: userID
  119. })
  120. })
  121. // this.trtcRoomContext.on(EVENT.LOCAL_AUDIO_VOLUME_UPDATE, event => {
  122. // console.log(event)
  123. // })
  124. console.error('房间号', getApp().globalData.roomId)
  125. this.trtcRoomContext.enterRoom({
  126. roomID: getApp().globalData.roomId
  127. }).then(() => {
  128. console.log('成功进入房间')
  129. }).catch((res) => {
  130. console.error('room joinRoom 进房失败:', res)
  131. this.hasEnter = false
  132. })
  133. let timer = null
  134. timer = setInterval(() => {
  135. console.error('轮询', this.successEnter)
  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. console.log('重试')
  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. })