answer.html 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <!DOCTYPE html>
  2. <html lang="zh">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>视频通话</title>
  7. <link rel="stylesheet" href="./css/video-call.css" />
  8. </head>
  9. <body>
  10. <div id="app">
  11. <video ref="mainVideo" poster="./images/mt/pt.png" id="vd_user" autoplay class="main-video"></video>
  12. <div v-if="!friendData.video" class="friend-info">
  13. <img :src="friendAvatar" style="width: 60px;height:60px;border-radius: 5px;margin-top: 100px;" alt=""/>
  14. <div class="friend-name">{{ friendName }}</div>
  15. <div class="friend-tips">{{isConnect?'正在通话中...':'正在等待接听'}}</div>
  16. </div>
  17. <video ref="localVideo" :muted="true" poster="./images/mt/pt.png" autoplay class="local-video"></video>
  18. <div class="video-menus">
  19. <div>
  20. <img @click="closeCalling" src="./images/mt/icon_off.png" class="menu-close" alt=""/>
  21. </div>
  22. <div v-if="showAccept && !isConnect">
  23. <img @click="accept" src="./images/mt/icon_on.png" class="menu-close" alt=""/>
  24. </div>
  25. </div>
  26. </div>
  27. <audio id="bgSound" src="./images/calling.mp3" autoplay loop style='display: none'></audio>
  28. <script src="./js/vue.global.js"></script>
  29. <script src="./js/uni.webview.1.5.4.js"></script>
  30. <script src="./js/peerjs.min.js"></script>
  31. <script src="./js/video-call.js"></script>
  32. <script>
  33. const {createApp, ref, onMounted} = Vue
  34. document.addEventListener('UniAppJSBridgeReady', function () {
  35. uni.getEnv(function (res) {
  36. createApp({
  37. setup() {
  38. const id = ref('')
  39. const name = ref('')
  40. const peerId = ref('')
  41. const localPeerId = ref(undefined)
  42. const friendId = ref('')
  43. const friendAvatar = ref('')
  44. const friendName = ref('')
  45. const localData = ref({
  46. audio: false,
  47. video: false
  48. })
  49. const friendData = ref({
  50. audio: false,
  51. video: false
  52. })
  53. const speaker = ref(true)
  54. const showVideo = ref(false)
  55. const isConnect = ref(false)
  56. const showAccept = ref(false)
  57. const mainVideo = ref()
  58. const localVideo = ref()
  59. const localMedia = ref()
  60. const dataConnection = ref()
  61. onMounted(() => {
  62. id.value = getQueryVariable("id")
  63. name.value = decodeURI(getQueryVariable("name"))
  64. peerId.value = getQueryVariable("peerId")
  65. friendId.value = getQueryVariable("friendId")
  66. friendName.value = decodeURI(getQueryVariable("friendName"))
  67. friendAvatar.value = getQueryVariable("friendAvatar")
  68. showVideo.value = getQueryVariable("showVideo") === 'true'
  69. const localPeer = initPeer()
  70. localPeer.on('open', (pid) => {
  71. localPeerId.value = pid
  72. dataConnection.value = localPeer.connect(peerId.value)
  73. dataConnection.value.on('open', () => {
  74. showAccept.value = true
  75. })
  76. dataConnection.value.on('data', (data) => {
  77. if (data.cmd === PeerCmd.ringOff) {
  78. closeCalling()
  79. } else if (data.cmd === PeerCmd.busy) {
  80. closeCalling()
  81. } else if (data.cmd === PeerCmd.accept) {
  82. getLocalUserMedia({ audio: true, video: showVideo.value })
  83. .then((userMedia) => {
  84. localMedia.value = userMedia
  85. friendData.value.video = showVideo.value
  86. isConnect.value = true
  87. const mediaConnection = localPeer.call(peerId.value, userMedia)
  88. mediaConnection.on('stream', (otherUserMedia) => {
  89. localData.value.video = showVideo.value
  90. mainVideo.value.srcObject = otherUserMedia
  91. mainVideo.value.muted = false
  92. if (showVideo.value) {
  93. localVideo.value.srcObject = userMedia
  94. localVideo.value.muted = true
  95. }
  96. })
  97. })
  98. .catch(() => {
  99. isConnect.value = false
  100. closeCalling()
  101. })
  102. }
  103. })
  104. })
  105. })
  106. const muteAudio = (boo) => {
  107. console.log('123',boo)
  108. }
  109. const changeSpeaker = () => {
  110. }
  111. const changeCamera = () => {
  112. console.log('changeCamera')
  113. }
  114. const closeCalling = () => {
  115. //关闭视频流
  116. localMedia.value?.getTracks().forEach((track) => {
  117. track.stop()
  118. })
  119. uni.postMessage({
  120. data: {
  121. type: 'close',
  122. data:{calling:false}
  123. }
  124. })
  125. }
  126. /**
  127. * 接受视频
  128. */
  129. const accept = () => {
  130. uni.postMessage({
  131. data: {
  132. type: 'accept',
  133. data:{
  134. peerId: localPeerId.value
  135. }
  136. }
  137. })
  138. document.getElementById('bgSound').pause()
  139. //发送请求,告诉对方我要视频
  140. dataConnection.value?.send({
  141. cmd: PeerCmd.request
  142. })
  143. }
  144. return {
  145. id,
  146. name,
  147. friendId,
  148. friendAvatar,
  149. friendName,
  150. localData,
  151. showVideo,
  152. friendData,
  153. isConnect,
  154. showAccept,
  155. changeCamera,
  156. changeSpeaker,
  157. muteAudio,
  158. mainVideo,
  159. localVideo,
  160. closeCalling,
  161. accept,
  162. speaker
  163. }
  164. }
  165. }).mount('#app')
  166. });
  167. });
  168. </script>
  169. </body>
  170. </html>