pickVideo.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <template>
  2. <view class="container">
  3. <trtc-room ref="trtc-component" :config="rtcConfig"> </trtc-room>
  4. <view class="tip-toast" v-if="showTipToast">
  5. <view>当前房间为1v1双人通话房间</view>
  6. <view>不希望其他人打扰</view>
  7. </view>
  8. <u-top-tips ref="uTips"></u-top-tips>
  9. </view>
  10. </template>
  11. <script>
  12. const NET = require('@/utils/request')
  13. const API = require('@/config/api')
  14. import {
  15. genTestUserSig,
  16. setData
  17. } from "@/pagesMedia/debug/GenerateTestUserSig";
  18. import trtcRoom from "@/pagesMedia/trtc-room/trtc-room";
  19. export default {
  20. components: {
  21. trtcRoom
  22. },
  23. data() {
  24. return {
  25. liveId: '',
  26. roomId: '',
  27. rtcConfig: {
  28. sdkAppID: '',
  29. // 必要参数 开通实时音视频服务创建应用后分配的 sdkAppID
  30. userID: '',
  31. // 必要参数 用户 ID 可以由您的帐号系统指定
  32. userSig: '',
  33. // 必要参数 身份签名,相当于登录密码的作用
  34. template: '' // 必要参数 组件模版,支持的值 1v1 grid custom ,注意:不支持动态修改, iOS 不支持 pusher 动态渲染
  35. },
  36. showTipToast: false,
  37. options: null
  38. }
  39. },
  40. onLoad() {
  41. wx.setKeepScreenOn({
  42. keepScreenOn: true
  43. }); // 获取 rtcroom 实例
  44. this.options = {
  45. template: '1v1'
  46. }
  47. NET.request(API.creatPickVideo, {}, 'GET').then(res => {
  48. this.liveId = res.data.liveId
  49. this.roomId = Number(res.data.roomId)
  50. this.trtcComponent = this.$refs['trtc-component'] // 监听TRTC Room 关键事件
  51. this.enterRoom({
  52. roomID: this.roomId,
  53. userID: uni.getStorageSync("userData").userId, //设置为用户id
  54. template: '1v1',
  55. debugMode: false,
  56. cloudenv: 'PRO'
  57. });
  58. this.bindTRTCRoomEvent(); // 将String 类型的 true false 转换成 boolean
  59. }).catch(error => {
  60. this.$refs.uTips.show({
  61. title: error.data.msg,
  62. type: 'warning',
  63. })
  64. })
  65. },
  66. methods: {
  67. setData,
  68. enterRoom: function(params) {
  69. params.template = params.template || '1v1';
  70. params.roomID = params.roomID || 2333;
  71. params.userID = params.userID || new Date().getTime().toString(16);
  72. console.log('* room enterRoom', params);
  73. const Signature = genTestUserSig(params.userID);
  74. params.sdkAppID = Signature.sdkAppID;
  75. params.userSig = Signature.userSig;
  76. this.template = params.template;
  77. this.rtcConfig = {
  78. sdkAppID: params.sdkAppID,
  79. // 您的实时音视频服务创建应用后分配的 sdkAppID
  80. userID: params.userID,
  81. userSig: params.userSig,
  82. template: params.template,
  83. // 1v1 grid custom
  84. debugMode: params.debugMode,
  85. // 非必要参数,打开组件的调试模式,开发调试时建议设置为 true
  86. beautyLevel: 9 // 默认开启美颜
  87. // cloudenv: params.cloudenv, // 非必要参数
  88. };
  89. this.setData({
  90. rtcConfig: this.rtcConfig
  91. }, () => {
  92. // roomID 取值范围 1 ~ 4294967295
  93. this.trtcComponent.enterRoom({
  94. roomID: params.roomID
  95. }).then(() => {
  96. }).catch(res => {
  97. console.error('* room joinRoom 进房失败:', res);
  98. });
  99. });
  100. },
  101. bindTRTCRoomEvent: function() {
  102. const TRTC_EVENT = this.trtcComponent.EVENT;
  103. this.timestamp = []; // 初始化事件订阅
  104. this.trtcComponent.on(TRTC_EVENT.LOCAL_JOIN, event => {
  105. console.log('* room LOCAL_JOIN', event); // 进房成功,触发该事件后可以对本地视频和音频进行设置
  106. if (this.options.localVideo === true || this.options.template === '1v1') {
  107. this.trtcComponent.publishLocalVideo();
  108. }
  109. if (this.options.localAudio === true || this.options.template === '1v1') {
  110. this.trtcComponent.publishLocalAudio();
  111. }
  112. });
  113. this.trtcComponent.on(TRTC_EVENT.LOCAL_LEAVE, event => {
  114. console.log('* room LOCAL_LEAVE', event);
  115. });
  116. this.trtcComponent.on(TRTC_EVENT.ERROR, event => {
  117. console.log('* room ERROR', event);
  118. }); // 远端用户进房
  119. this.trtcComponent.on(TRTC_EVENT.REMOTE_USER_JOIN, event => {
  120. console.log('* room REMOTE_USER_JOIN --- room.vue', event, this.trtcComponent.getRemoteUserList(), this.template);
  121. this.timestamp.push(new Date()); // 1v1视频通话时限制人数为两人的简易逻辑,建议通过后端实现房间人数管理
  122. // 2人以上同时进行通话请选择网格布局
  123. if (this.template === '1v1' && this.timestamp.length > 1) {
  124. const interval = this.timestamp[1] - this.timestamp[0];
  125. if (interval < 1000) {
  126. // 房间里已经有两个人
  127. this.setData({
  128. showTipToast: true
  129. }, () => {
  130. setTimeout(() => {
  131. this.setData({
  132. showTipToast: false
  133. });
  134. wx.navigateBack({
  135. delta: 1
  136. });
  137. }, 4000);
  138. });
  139. }
  140. }
  141. }); // 远端用户退出
  142. this.trtcComponent.on(TRTC_EVENT.REMOTE_USER_LEAVE, event => {
  143. console.log('* room REMOTE_USER_LEAVE', event, this.trtcComponent.getRemoteUserList());
  144. if (this.template === '1v1') {
  145. this.timestamp = [];
  146. }
  147. if (this.template === '1v1' && this.remoteUser === event.data.userID) {
  148. this.remoteUser = null;
  149. }
  150. }); // 远端用户推送视频
  151. this.trtcComponent.on(TRTC_EVENT.REMOTE_VIDEO_ADD, event => {
  152. console.log('* room REMOTE_VIDEO_ADD', event, this.trtcComponent.getRemoteUserList()); // 订阅视频
  153. const userList = this.trtcComponent.getRemoteUserList();
  154. const data = event.data;
  155. if (this.template === '1v1' && (!this.remoteUser || this.remoteUser === data.userID)) {
  156. // 1v1 只订阅第一个远端流
  157. this.remoteUser = data.userID;
  158. this.trtcComponent.subscribeRemoteVideo({
  159. userID: data.userID,
  160. streamType: data.streamType
  161. });
  162. } else if (this.template === 'grid') {
  163. this.trtcComponent.subscribeRemoteVideo({
  164. userID: data.userID,
  165. streamType: data.streamType
  166. });
  167. }
  168. if (this.template === 'custom' && data.userID && data.streamType) {
  169. let index = userList.findIndex(item => {
  170. return item.userID === data.userID;
  171. });
  172. index++;
  173. const y = 320 * index + 160; // 设置远端视图坐标和尺寸
  174. this.trtcComponent.setViewRect({
  175. userID: data.userID,
  176. streamType: data.streamType,
  177. xAxis: '480rpx',
  178. yAxis: y + 'rpx',
  179. width: '240rpx',
  180. height: '320rpx'
  181. });
  182. }
  183. }); // 远端用户取消推送视频
  184. this.trtcComponent.on(TRTC_EVENT.REMOTE_VIDEO_REMOVE, event => {
  185. console.log('* room REMOTE_VIDEO_REMOVE', event, this.trtcComponent.getRemoteUserList());
  186. }); // 远端用户推送音频
  187. this.trtcComponent.on(TRTC_EVENT.REMOTE_AUDIO_ADD, event => {
  188. console.log('* room REMOTE_AUDIO_ADD', event, this.trtcComponent.getRemoteUserList()); // 订阅音频
  189. const data = event.data;
  190. if (this.template === '1v1' && (!this.remoteUser || this.remoteUser === data.userID)) {
  191. this.remoteUser = data.userID;
  192. this.trtcComponent.subscribeRemoteAudio({
  193. userID: data.userID
  194. });
  195. } else if (this.template === 'grid' || this.template === 'custom') {
  196. this.trtcComponent.subscribeRemoteAudio({
  197. userID: data.userID
  198. });
  199. } // 如果不订阅就不会自动播放音频
  200. // this.trtcComponent.subscribeRemoteAudio({ userID: data.userID })
  201. }); // 远端用户取消推送音频
  202. this.trtcComponent.on(TRTC_EVENT.REMOTE_AUDIO_REMOVE, event => {
  203. console.log('* room REMOTE_AUDIO_REMOVE', event, this.trtcComponent.getRemoteUserList());
  204. });
  205. }
  206. }
  207. }
  208. </script>
  209. <style lang="less" scoped>
  210. page {
  211. width: 100%;
  212. height: 100%;
  213. }
  214. .container {
  215. width: 100%;
  216. height: 100%;
  217. float: left;
  218. position: relative;
  219. .tip-toast {
  220. position: absolute;
  221. top: 40vh;
  222. width: 70vw;
  223. left: 15vw;
  224. border-radius: 12rpx;
  225. height: 20vh;
  226. background: rgba(0, 0, 0, 0.8);
  227. color: white;
  228. display: flex;
  229. flex-direction: column;
  230. align-items: center;
  231. justify-content: center;
  232. }
  233. .tip-toast view {
  234. padding: 20rpx 0;
  235. font-size: 32rpx;
  236. }
  237. }
  238. </style>