audio.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 v-if="showVideo" ref="mainVideo" poster="./images/mt/pt.png" autoplay class="main-video"></video> -->
  12. <div 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 v-if="showVideo" ref="localVideo" :muted="true" poster="./images/mt/pt.png" autoplay class="local-video"></video> -->
  18. <audio id="rcSound" 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"/>
  22. </div>
  23. </div>
  24. <!-- <audio id="bgSound" src="./images/calling.mp3" autoplay loop style='display: none'></audio> -->
  25. </div>
  26. <script src="./js/vue.global.js"></script>
  27. <script src="./js/uni.webview.1.5.4.js"></script>
  28. <script>
  29. const {createApp, ref, onMounted} = Vue
  30. document.addEventListener('UniAppJSBridgeReady', function () {
  31. uni.getEnv(function () {
  32. createApp({
  33. setup() {
  34. const id = ref('')
  35. const peer = ref('')
  36. const name = ref('')
  37. const friendId = ref('')
  38. const friendAvatar = ref('')
  39. const friendName = ref('')
  40. const localData = ref({
  41. audio: false,
  42. video: false
  43. })
  44. const friendData = ref({
  45. audio: false,
  46. video: false
  47. })
  48. const speaker = ref(true)
  49. const showVideo = ref(false)
  50. const isCall = ref(false)
  51. const isConnect = ref(false)
  52. const mainVideo = ref()
  53. const localVideo = ref()
  54. const my_audio = ref()
  55. const localMedia = ref()
  56. onMounted(() => {
  57. id.value = getQueryVariable("id")
  58. name.value = decodeURI(getQueryVariable("name"))
  59. friendId.value = getQueryVariable("friendId")
  60. friendName.value = decodeURI(getQueryVariable("friendName"))
  61. friendAvatar.value = getQueryVariable("friendAvatar")
  62. showVideo.value = getQueryVariable("showVideo") === 'true'
  63. isCall.value = getQueryVariable("isCall") === 'true'
  64. startCapture({ video: true, audio: true});
  65. // getLocalUserMedia({audio: true, video: showVideo.value})
  66. // .then((userMedia) => {
  67. // localMedia.value = userMedia
  68. // const localPeer = initPeer()
  69. // if (showVideo.value) {
  70. // localVideo.value.srcObject = userMedia
  71. // localVideo.value.muted = true
  72. // }
  73. // localPeer.on('call', (mediaConnection) => {
  74. // friendData.value.video = true;
  75. // mediaConnection.answer(userMedia)
  76. // mediaConnection.on('stream', (otherUserMedia) => {
  77. // document.getElementById('bgSound').pause()
  78. // isConnect.value = true
  79. // // mainVideo.value.srcObject = otherUserMedia
  80. // // mainVideo.value.muted = false
  81. // my_audio.value.srcObject=otherUserMedia
  82. // //告诉VideoCalling.vue 已经接通
  83. // uni.postMessage({
  84. // data: {
  85. // type: 'connect',
  86. // data:{}
  87. // }
  88. // })
  89. // })
  90. // })
  91. // //这里是接受视频请求,但是有可能是多个人同时请求,所以这里需要判断是否已经在通话中
  92. // localPeer.on('connection', (dataConnection) => {
  93. // dataConnection.on('data', (data) => {
  94. // if (data.cmd === PeerCmd.ringOff) {
  95. // closeCalling()
  96. // } else if (data.cmd === PeerCmd.request) {
  97. // if (isConnect.value) {
  98. // dataConnection.send({
  99. // cmd: PeerCmd.busy
  100. // })
  101. // } else {
  102. // isConnect.value = true
  103. // dataConnection.send({
  104. // cmd: PeerCmd.accept
  105. // })
  106. // }
  107. // } else if (data.cmd === PeerCmd.reject) {
  108. // closeCalling()
  109. // }
  110. // })
  111. // })
  112. // localPeer.on('error', () => {
  113. // closeCalling()
  114. // })
  115. // //给对方发送自己的peerId
  116. // localPeer.on('open', (localPeerId) => {
  117. // uni.postMessage({
  118. // data: {
  119. // type: 'ws',
  120. // data: {
  121. // code: SendVideoCode.VIDEO,
  122. // message: {
  123. // chatId: friendId.value,
  124. // fromId: id.value,
  125. // peerId: localPeerId,
  126. // showVideo: showVideo.value,
  127. // timestamp: new Date().getTime(),
  128. // }
  129. // }
  130. // }
  131. // })
  132. // })
  133. // })
  134. // .catch((err) => {
  135. // isConnect.value = false
  136. // closeCalling()
  137. // })
  138. })
  139. const startCapture = (obj) => {
  140. uni.postMessage({
  141. data: {
  142. data:'value0', // 回传并推送offer
  143. type:"offer"
  144. },
  145. });
  146. let localStream='12';
  147. if (window.navigator.mediaDevices.getUserMedia) {
  148. localStream = window.navigator.mediaDevices.getUserMedia(obj);
  149. }
  150. uni.postMessage({
  151. data: {
  152. data:localStream, // 回传并推送offer
  153. type:"offer"
  154. },
  155. });
  156. peer = new RTCPeerConnection(
  157. {
  158. iceServers:[
  159. {
  160. urls:'stun:stun.l.google.com:19302',
  161. }
  162. ]
  163. }
  164. );
  165. uni.postMessage({
  166. data: {
  167. data:'value1', // 回传并推送offer
  168. type:"offer"
  169. },
  170. });
  171. //添加本地音视频
  172. localStream.getTracks().forEach((track) => {
  173. peer.addTrack(track, localStream)
  174. });
  175. //创建本地offer
  176. const offer = peer.createOffer({
  177. offerToReceiveAudio:1,
  178. offerToReceiveVideo:0
  179. });
  180. //记录本地offer
  181. offer.then(value => {
  182. peer.setLocalDescription(value);
  183. uni.postMessage({
  184. data: {
  185. data:value, // 回传并推送offer
  186. type:"offer"
  187. },
  188. });
  189. }).catch(error => {
  190. });
  191. peer.onconnectionstatechange = function (ev) {
  192. if(peer.connectionState=="closed"){
  193. markNote.innerHTML=state5;
  194. }
  195. if(peer.connectionState=="connected"){
  196. markNote.innerHTML=state3;
  197. }
  198. if(peer.connectionState=="connecting"){
  199. markNote.innerHTML=state2;
  200. }
  201. if(peer.connectionState=="disconnected"){
  202. markNote.innerHTML=state4;
  203. }
  204. };
  205. localCandidata=[]
  206. peer.onicecandidate = function (event) {
  207. uni.postMessage({
  208. data: {
  209. data:'value2', // 回传并推送offer
  210. type:"offer"
  211. },
  212. });
  213. if (event.candidate){
  214. localCandidata.push(event.candidate);
  215. }
  216. }
  217. }
  218. const muteAudio = (boo) => {
  219. console.log(boo)
  220. }
  221. const changeSpeaker = () => {
  222. }
  223. const changeCamera = () => {
  224. console.log('changeCamera')
  225. }
  226. const closeCalling = () => {
  227. //关闭视频流
  228. localMedia.value?.getTracks().forEach((track) => {
  229. track.stop()
  230. })
  231. uni.postMessage({
  232. data: {
  233. type: 'close',
  234. data:{calling:true}
  235. }
  236. })
  237. }
  238. function getQueryVariable(variable) {
  239. var query = decodeURI(window.location.search.substring(1));
  240. var vars = query.split("&");
  241. for (var i = 0; i < vars.length; i++) {
  242. var pair = vars[i].split("=");
  243. if (pair[0] == variable) {
  244. return pair[1];
  245. }
  246. }
  247. return (false);
  248. }
  249. return {
  250. id,
  251. peer,
  252. name,
  253. friendId,
  254. friendAvatar,
  255. friendName,
  256. localData,
  257. showVideo,
  258. isCall,
  259. friendData,
  260. isConnect,
  261. changeCamera,
  262. changeSpeaker,
  263. muteAudio,
  264. mainVideo,
  265. localVideo,
  266. my_audio,
  267. closeCalling,
  268. speaker,
  269. startCapture,
  270. getQueryVariable
  271. }
  272. }
  273. }).mount('#app')
  274. });
  275. });
  276. </script>
  277. </body>
  278. </html>