answerAudio.html 7.8 KB

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