audioanswer.html 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <link rel="stylesheet" type="text/css" href="./css/index.css" />
  8. <title>语音通话</title>
  9. <style>
  10. html, body {
  11. height: 100%;
  12. }
  13. body {
  14. margin: 0;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <!-- <video class="Bvideo" id="Big_video" preload autoplay onclick="Bvideoselc()"></video> -->
  20. <!-- <video class="Svideo" id="sm_video" preload muted autoplay onclick="Svideoselc()"></video> -->
  21. <audio id="my_audio" autoplay></audio>
  22. <div class="contentColumnC" style="width: 100%;margin-top: 60px;"">
  23. <img id="Avatar" class="iconImg" style="margin-top: 5px;" src="./img/logo.png" alt=""/>
  24. <div class="contentInRowC" style="width: 100%;margin-top: 10px;">
  25. <div id="Name"></div>
  26. </div>
  27. </div>
  28. <div class="contentInRowC" style="width: 100%;margin-top: 40px;">
  29. <div id="Note"></div>
  30. <img class="calling" src="./img/loading2.gif" alt="" />
  31. </div>
  32. <div style="width: 100%;margin-top: 100px;">
  33. <div class="contentInRowC">
  34. <img class="guadanImg" style="margin-top: 5px;" src="./img/icon_off.png" alt="" onclick="guaduan()"/>
  35. <img id='dianhua_Img' class="guadanImg" style="margin-top: 5px;margin-left: 30px;" src="./img/icon_on.png" alt="" onclick="actjietong()"/>
  36. </div>
  37. </div>
  38. </body>
  39. <script type="text/javascript" src="./js/lib/uniwebviewsdk.js"></script>
  40. <script>
  41. let state1,state2,state3,state4,state5;
  42. const markNote = document.getElementById('Note');
  43. let localStream = null;
  44. let localCall = 0;
  45. let localCandidata=null;
  46. let peer = null;
  47. let video=null;
  48. let jietong=false;
  49. let payload=null;
  50. let markindex=0;
  51. // const Big_video = document.getElementById('Big_video');
  52. // const sm_video = document.getElementById('sm_video');
  53. // const my_audio = document.getElementById('my_audio');
  54. const dianhua_Img = document.getElementById('dianhua_Img');
  55. //====================================================
  56. function getQueryVariable(variable) {
  57. var query = decodeURI(window.location.search.substring(1));
  58. var vars = query.split("&");
  59. for (var i = 0; i < vars.length; i++) {
  60. var pair = vars[i].split("=");
  61. if (pair[0] == variable) {
  62. return pair[1];
  63. }
  64. }
  65. return (false);
  66. }
  67. function Bvideoselc(){
  68. if(localStream!=null){
  69. //Big_video.srcObject =localStream;
  70. Big_video.setAttribute('autoplay', true); /* THIS */
  71. }
  72. }
  73. function Svideoselc(){
  74. if(localStream!=null){
  75. //sm_video.srcObject =localStream;
  76. sm_video.setAttribute('autoplay', true); /* THIS */
  77. }
  78. }
  79. function actjietong(){
  80. jietong=true;
  81. dianhua_Img.style.display='none'
  82. markNote.innerHTML=state2;
  83. uni.postMessage({
  84. data: {
  85. data:'', // 回传
  86. type:'accept'
  87. },
  88. });
  89. }
  90. function guaduan(){
  91. if(peer){
  92. peer.close();
  93. }
  94. uni.postMessage({
  95. data: {
  96. data:'', // 回传
  97. type:'close'
  98. },
  99. });
  100. }
  101. //====================================================
  102. function appAct (obj) {
  103. if(obj.type=='offer'){//收到发起方sdp offer
  104. if(jietong){
  105. //markNote.innerHTML=state2;
  106. RCstartWebRct(obj);//启动wbrtc
  107. }
  108. }
  109. }
  110. function appActcandidate (obj){//发起方收到candidate 添加到Rct
  111. for(var i=0;i<obj.length;i++){
  112. peer.addIceCandidate(obj[i]);
  113. }
  114. //推送candidate
  115. uni.postMessage({
  116. data: {
  117. data:localCandidata, // 回传并推送offer
  118. type:"candidate"
  119. },
  120. });
  121. }
  122. async function startCapture(displayMediaOptions) {
  123. try {
  124. localStream = await navigator.mediaDevices.getUserMedia(displayMediaOptions);
  125. //actjietong();
  126. } catch(err) {
  127. console.error(err);
  128. }
  129. }
  130. function startCommunicate(){
  131. peer.ontrack = (e) => {
  132. if(e.streams[0]){
  133. var otherVideos = document.querySelector('#my_audio');
  134. //var otherVideos = document.querySelector('#Big_video');
  135. otherVideos.srcObject = e.streams[0];
  136. //otherVideos.setAttribute('autoplay', true); /* THIS */
  137. uni.postMessage({
  138. data: {
  139. data:JSON.stringify(e.streams[0]), // 回传并推送offer
  140. type:'sss'
  141. },
  142. });
  143. }
  144. // sm_video.srcObject =localStream;
  145. // sm_video.setAttribute('autoplay', true); /* THIS */
  146. }
  147. }
  148. async function RCstartWebRct(payload){
  149. peer = new RTCPeerConnection(
  150. {
  151. iceServers:[
  152. {
  153. urls: ["stun:203.175.169.52:3478",
  154. "turn:203.175.169.52:3478"
  155. ],
  156. username: 'aaaaa',
  157. credential: 'bbbbb'
  158. }
  159. ],
  160. offerExtmapAllowMixed:false
  161. }
  162. );
  163. //添加本地音视频
  164. localStream.getTracks().forEach((track) => {
  165. peer.addTrack(track, localStream)
  166. })
  167. localCandidata=[];
  168. peer.onicecandidate = function (event) {
  169. if (event.candidate){
  170. localCandidata.push(event.candidate);
  171. }
  172. }
  173. startCommunicate();
  174. peer.onconnectionstatechange = function (ev) {
  175. if(peer.connectionState=="closed"){
  176. markNote.innerHTML=state5;
  177. }
  178. if(peer.connectionState=="connected"){
  179. markNote.innerHTML=state3;
  180. }
  181. if(peer.connectionState=="connecting"){
  182. markNote.innerHTML=state2;
  183. }
  184. if(peer.connectionState=="disconnected"){
  185. markNote.innerHTML=state4;
  186. }
  187. };
  188. //记录远端offer
  189. const offer = new RTCSessionDescription(payload);
  190. await peer.setRemoteDescription(offer);
  191. const remoteAnswer = await peer.createAnswer();
  192. //记录本地offer
  193. await peer.setLocalDescription(remoteAnswer);
  194. //推送remoteAnswer
  195. uni.postMessage({
  196. data: {
  197. data:remoteAnswer, // 回传并推送offer
  198. type:"answer"
  199. },
  200. });
  201. }
  202. function initRtc() {
  203. title='语音通话';
  204. state1='正在呼叫';
  205. state2='正在接通';
  206. state3='已接通';
  207. state4='已挂断';
  208. state5='结束通话';
  209. document.getElementById('Name').innerHTML=getQueryVariable('friendName');;
  210. document.getElementById('Avatar').src=getQueryVariable('friendAvatar');;
  211. markNote.innerHTML=state1;
  212. setTimeout(() => {
  213. //startCapture({ video: true, audio: true});
  214. startCapture({ video: false, audio: true});
  215. }, 1000)
  216. return;
  217. }
  218. function releaseTack(){
  219. if(peer){
  220. peer.close();
  221. }
  222. }
  223. window.onload=initRtc;
  224. window.onunload=releaseTack;
  225. </script>
  226. </html>