pickVideo.vue 10 KB

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