voice.js 4.5 KB

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