voice.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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('hasEnter', this.hasEnter)
  76. this.hasEnter = true
  77. getApp().globalData.audioUser = user
  78. console.log(user, 'user')
  79. this.setData({
  80. trtcConfig: {
  81. scene: 'rtc',
  82. userID: user.userId, // 用户 ID,可以由您的帐号系统指定
  83. // sdkAppID: '1400431163', // 开通实时音视频服务创建应用后分配的 SDKAppID
  84. // userSig: user.sig, // 身份签名,相当于登录密码的作用
  85. sdkAppID: "1400685498",
  86. userSig: 'eJwtzcsOgjAQBdB-6RaDU2jLI3EhsrHRBdGNK2Po0IyvNAWJxvjvEmA5597c*bLj7hD26FnOohDYYrzJ4LOjhkZ*tejPd7wY9EJwnsV8brXmdnGODMu5AEggExBNSUcPHFQpnkopIJkU34784ApECjBvkB1e8MLJfWyLOAis06Wut4b0tVdL77RVTb35dGXby6o6rVfs9wdhLDRy',
  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.noMute) {
  102. this.unpublishLocalAudio()
  103. } else {
  104. this.publishLocalAudio()
  105. }
  106. })
  107. this.trtcRoomContext.on(EVENT.LOCAL_LEAVE, (event) => {
  108. console.error('离开房间')
  109. })
  110. // 监听远端用户的音频流的变更事件
  111. this.trtcRoomContext.on(EVENT.REMOTE_AUDIO_ADD, (event) => {
  112. // 订阅(即播放)远端用户的音频流
  113. let userID = event.data.userID
  114. this.trtcRoomContext.subscribeRemoteAudio({
  115. userID: userID
  116. })
  117. })
  118. // this.trtcRoomContext.on(EVENT.LOCAL_AUDIO_VOLUME_UPDATE, event => {
  119. // console.log(event)
  120. // })
  121. console.error('join')
  122. this.trtcRoomContext.enterRoom({
  123. roomID: getApp().globalData.roomId
  124. }).then(() => {
  125. console.log('成功进入房间')
  126. }).catch((res) => {
  127. console.error('room joinRoom 进房失败:', res)
  128. this.hasEnter = false
  129. })
  130. let timer = null
  131. timer = setInterval(() => {
  132. if (!this.successEnter) {
  133. this.trtcRoomContext.enterRoom({
  134. roomID: getApp().globalData.roomId
  135. }).then(() => {
  136. console.log('成功进入房间')
  137. }).catch((res) => {
  138. console.error('room joinRoom 进房失败:', res)
  139. this.hasEnter = false
  140. })
  141. } else {
  142. clearInterval(timer)
  143. timer = null
  144. }
  145. }, 3000);
  146. }
  147. })
  148. },
  149. unpublishLocalAudio () {
  150. this.trtcRoomContext && this.trtcRoomContext.unpublishLocalAudio()
  151. },
  152. publishLocalAudio () {
  153. this.trtcRoomContext && this.trtcRoomContext.publishLocalAudio()
  154. }
  155. }
  156. })