call.nvue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <template>
  2. <view class="call-view">
  3. <view class="nav-bar">
  4. <text class="duration">{{duration}}</text>
  5. </view>
  6. <!-- 音频通话 -->
  7. <view v-if="currentCall.mediaType===0" class="profile">
  8. <image class="user-avatar" mode="widthFix" :src="friend.data.avatar"></image>
  9. <text class="user-name">{{friend.data.name}}</text>
  10. </view>
  11. <!-- 视频通话 -->
  12. <view v-if="currentCall.mediaType === 1" class="content">
  13. <view class="container">
  14. <!-- 大视图 -->
  15. <!-- #ifdef APP -->
  16. <grtc-video v-if="!mainVideoUser.cameraMuted" :userId="mainVideoUser.id" class="main-video"></grtc-video>
  17. <!-- #endif -->
  18. <!-- #ifdef H5 -->
  19. <web-grtc-video v-if="!mainVideoUser.cameraMuted" :userId="mainVideoUser.id" class="main-video"></web-grtc-video>
  20. <!-- #endif -->
  21. <view v-if="mainVideoUser.cameraMuted" class="main-video">
  22. <image class="main-avatar" mode="widthFix" :src="mainVideoUser.data.avatar"></image>
  23. </view>
  24. <!-- 小视图 -->
  25. <!-- #ifdef APP -->
  26. <grtc-video v-if="!miniVideoUser.cameraMuted" @click="toggleUser()" :userId="miniVideoUser.id" class="mini-video"></grtc-video>
  27. <!-- #endif -->
  28. <!-- #ifdef H5 -->
  29. <web-grtc-video v-if="!miniVideoUser.cameraMuted" @click="toggleUser()" :userId="miniVideoUser.id" class="mini-video"></web-grtc-video>
  30. <!-- #endif -->
  31. <view v-if="miniVideoUser.cameraMuted" class="mini-video">
  32. <image class="mini-avatar" mode="widthFix" :src="miniVideoUser.data.avatar"></image>
  33. </view>
  34. </view>
  35. </view>
  36. <view class="action-box">
  37. <view class="action-top">
  38. <view class="action-item" @click="toggleMic()">
  39. <view v-if="self.micMuted">
  40. <view class="item-background-disable">
  41. <image class="action-bar-icon" src="/static/images/microphone-disable.png"></image>
  42. </view>
  43. <text class="action-text">麦克风已关</text>
  44. </view>
  45. <view v-else>
  46. <view class="item-background">
  47. <image class="action-bar-icon" src="/static/images/microphone.png"></image>
  48. </view>
  49. <text class="action-text">麦克风已开</text>
  50. </view>
  51. </view>
  52. <view class="action-item" @click="toggleSpeaker()">
  53. <view v-if="currentCall.speakerOn">
  54. <view class="item-background">
  55. <image class="action-bar-icon" src="/static/images/speaker.png"></image>
  56. </view>
  57. <text class="action-text">扬声器已开</text>
  58. </view>
  59. <view v-else>
  60. <view class="item-background-disable">
  61. <image class="action-bar-icon" src="/static/images/speaker-disable.png"></image>
  62. </view>
  63. <text class="action-text">扬声器已关</text>
  64. </view>
  65. </view>
  66. <view class="action-item" @click="toggleCamera()" v-if="currentCall.mediaType === 1">
  67. <view v-if="self.cameraMuted">
  68. <view class="item-background-disable">
  69. <image class="action-bar-icon" src="/static/images/camera-disable.png"></image>
  70. </view>
  71. <text class="action-text">摄像头已关</text>
  72. </view>
  73. <view v-else>
  74. <view class="item-background">
  75. <image class="action-bar-icon" src="/static/images/camera.png"></image>
  76. </view>
  77. <text class="action-text">摄像头已开</text>
  78. </view>
  79. </view>
  80. </view>
  81. <view class="action-item" @click="end()">
  82. <view class="end-background">
  83. <image class="action-icon" src="/static/images/phoneEnd.png"></image>
  84. </view>
  85. </view>
  86. <view class="switch-camera" @click="switchCamera()" v-if="currentCall.mediaType === 1">
  87. <image class="switch-camera-icon" src="/static/images/switch-camera.png"></image>
  88. </view>
  89. </view>
  90. </view>
  91. </template>
  92. <script>
  93. import webGrtcVideo from '../components/web-grtc-video/web-grtc-video.vue';
  94. const GRTC = uni.$GRTC;
  95. let interval = null;
  96. export default {
  97. data() {
  98. return {
  99. currentCall: GRTC.currentCall(),
  100. self: null,
  101. friend: null,
  102. mainVideoUser: null,
  103. miniVideoUser: null,
  104. duration: null
  105. }
  106. },
  107. components: {
  108. webGrtcVideo,
  109. },
  110. onLoad() {
  111. const isCaller = uni.$currentUser.id === this.currentCall.caller.id;
  112. this.self = this.getSelf();
  113. this.friend = this.getFriend();
  114. this.miniVideoUser = this.self;
  115. this.mainVideoUser = this.friend;
  116. this.duration = this.formattedDuration();
  117. interval = setInterval(() => {
  118. this.duration = this.formattedDuration();
  119. }, 1000);
  120. this.initListener();
  121. },
  122. onUnload() {
  123. GRTC.off(GRTC.EVENT.USER_QUIT, this.onUserQuit);
  124. GRTC.off(GRTC.EVENT.USER_MIC_CHANGED, this.onUserMicChanged);
  125. GRTC.off(GRTC.EVENT.USER_CAMERA_CHANGED, this.onUserCameraChanged);
  126. },
  127. onBackPress(event) {
  128. return event.from === 'backbutton'; // 禁止安卓侧滑返回
  129. },
  130. methods: {
  131. getSelf() {
  132. if (uni.$currentUser.id === this.currentCall.caller.id) {
  133. return this.currentCall.caller
  134. }
  135. return this.currentCall.callees[0]
  136. },
  137. getFriend() {
  138. if (uni.$currentUser.id === this.currentCall.caller.id) {
  139. return this.currentCall.callees[0]
  140. }
  141. return this.currentCall.caller
  142. },
  143. initListener() {
  144. GRTC.on(GRTC.EVENT.USER_QUIT, this.onUserQuit);
  145. GRTC.on(GRTC.EVENT.USER_MIC_CHANGED, this.onUserMicChanged);
  146. GRTC.on(GRTC.EVENT.USER_CAMERA_CHANGED, this.onUserCameraChanged);
  147. },
  148. onUserQuit(event) {
  149. clearInterval(interval);
  150. const username = event.user.id === this.self.id ? '' : event.user.data.name;
  151. let message = '';
  152. switch (event.reason) {
  153. case 'GOEASY_DISCONNECTED':
  154. message = `${username}网络异常`;
  155. break;
  156. case 'HUNG_UP':
  157. message = `${username}已挂断`;
  158. break;
  159. }
  160. uni.showToast({
  161. title: message,
  162. icon: "none"
  163. });
  164. uni.navigateBack();
  165. },
  166. onUserMicChanged(event) {
  167. console.log("onUserMicChange",event);
  168. },
  169. onUserCameraChanged(event) {
  170. console.log("onUserCameraChanged",event);
  171. },
  172. end() {
  173. GRTC.end();
  174. },
  175. switchCamera() {
  176. GRTC.switchCamera();
  177. },
  178. toggleSpeaker() {
  179. GRTC.toggleSpeaker(!this.currentCall.speakerOn);
  180. },
  181. toggleMic() {
  182. GRTC.muteMic(!this.self.micMuted);
  183. },
  184. toggleCamera() {
  185. GRTC.muteCamera(!this.self.cameraMuted);
  186. },
  187. toggleUser() {
  188. const temp = this.mainVideoUser;
  189. this.mainVideoUser = this.miniVideoUser;
  190. this.miniVideoUser = temp;
  191. },
  192. formattedDuration() {
  193. const seconds = this.currentCall.getDuration();
  194. const minutes = Math.floor(seconds / 60);
  195. const formattedSeconds = String(seconds % 60).padStart(2, '0');
  196. const formattedMinutes = String(minutes).padStart(2, '0');
  197. return `${formattedMinutes}:${formattedSeconds}`; // mm:ss
  198. }
  199. }
  200. }
  201. </script>
  202. <style scoped>
  203. @import url('/static/style/rtcCallStyle.css');
  204. </style>