audio.html 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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}}</title>
  9. <style>
  10. html, body {
  11. height: 100%;
  12. }
  13. body {
  14. margin: 0;
  15. }
  16. video {
  17. width: 100%;
  18. height: 100%;
  19. position: absolute;
  20. left: 0;
  21. top: 0;
  22. z-index: -1;
  23. }
  24. </style>
  25. </head>
  26. <body>
  27. <!-- <video class="Bvideo" id="Big_video" preload autoplay onclick="Bvideoselc()"></video> -->
  28. <!-- <video class="Svideo" id="sm_video" preload muted autoplay onclick="Svideoselc()"></video> -->
  29. <audio id="my_audio" autoplay></audio>
  30. <div class="contentColumnC" style="width: 100%;margin-top: 60px;"">
  31. <div>{{ICnote}}</div>
  32. <img class="iconImg" style="margin-top: 5px;" src="./img/logo.png" alt="" onclick="guaduan()"/>
  33. <div class="contentInRowC" style="width: 100%;margin-top: 10px;">
  34. <div id="markNote"></div>
  35. <img class="calling" src="./img/loading2.gif" alt="" />
  36. </div>
  37. </div>
  38. <div style="width: 100%;margin-top: 100px;">
  39. <div class="contentColumnC">
  40. <img id='dianhua_Img' class="guadanImg" style="margin-top: 5px;" src="./img/jietong.png" alt="" onclick="guaduan()"/>
  41. </div>
  42. </div>
  43. </body>
  44. <script type="text/javascript" src="./js/lib/uniwebviewsdk.js"></script>
  45. <script>
  46. let language = getQueryVariable('language');
  47. let title,state1,state2,state3,state4,state5,ICnote;
  48. let localStream = null;
  49. let localCall = 0;
  50. let localCandidata=null;
  51. let peer = null;
  52. let video=null;
  53. let jietong=false;
  54. let payload=null;
  55. // const Big_video = document.getElementById('Big_video');
  56. // const sm_video = document.getElementById('sm_video');
  57. // const my_audio = document.getElementById('my_audio');
  58. const dianhua_Img = document.getElementById('dianhua_Img');
  59. const markNote = document.getElementById('markNote');
  60. //====================================================
  61. function Bvideoselc(){
  62. if(localStream!=null){
  63. //Big_video.srcObject =localStream;
  64. Big_video.setAttribute('autoplay', true); /* THIS */
  65. }
  66. }
  67. function Svideoselc(){
  68. if(localStream!=null){
  69. //sm_video.srcObject =localStream;
  70. sm_video.setAttribute('autoplay', true); /* THIS */
  71. }
  72. }
  73. function guaduan(){
  74. if(jietong){
  75. if(peer){
  76. peer.close();
  77. }
  78. uni.postMessage({
  79. data: {
  80. data:'', // 回传
  81. type:88
  82. },
  83. });
  84. }
  85. else{
  86. jietong=true;
  87. markNote.innerHTML=state2;
  88. RCstartWebRct(payload);
  89. dianhua_Img.src = "./img/guaduandd.png"
  90. }
  91. }
  92. //====================================================
  93. function appAct (obj) {
  94. if(obj.type==1){//主动发起im通话
  95. localCall=1;
  96. //startWebRct(obj.type);
  97. markNote.innerHTML=state1;
  98. }
  99. else if(obj.type==2){//收到推送消息
  100. if(localCall==1){
  101. markNote.innerHTML=state2;
  102. RCremoteData(obj.payload);
  103. }
  104. else{
  105. markNote.innerHTML=state1;
  106. payload=obj.payload;
  107. //RCstartWebRct(obj.payload);
  108. }
  109. }
  110. else if(obj.type==3){//收到candidate
  111. markNote.innerHTML=state2;
  112. for(var i=0;i<obj.payload.length;i++){
  113. peer.addIceCandidate(obj.payload[i]);
  114. }
  115. if(localCall!=1){
  116. uni.postMessage({
  117. data: {
  118. data:localCandidata, //应答方回应candidate
  119. type:3
  120. },
  121. });
  122. }
  123. }
  124. }
  125. async function startCapture(displayMediaOptions) {
  126. try {
  127. localStream = await navigator.mediaDevices.getUserMedia(displayMediaOptions);
  128. // Big_video.srcObject =localStream;
  129. // Big_video.setAttribute('autoplay', true); /* THIS */
  130. uni.postMessage({
  131. data: {
  132. data:'', // 回传
  133. type:99
  134. },
  135. });
  136. } catch(err) {
  137. console.error(err);
  138. }
  139. }
  140. async function startWebRct(type){
  141. peer = new RTCPeerConnection(
  142. {
  143. iceServers:[
  144. {
  145. urls:'stun:stun.l.google.com:19302',
  146. }
  147. ]
  148. }
  149. );
  150. //添加本地音视频
  151. localStream.getTracks().forEach((track) => {
  152. peer.addTrack(track, localStream)
  153. });
  154. //创建本地offer
  155. const offer = peer.createOffer({
  156. offerToReceiveAudio:1,
  157. offerToReceiveVideo:0
  158. });
  159. //记录本地offer
  160. offer.then(value => {
  161. peer.setLocalDescription(value);
  162. uni.postMessage({
  163. data: {
  164. data:value, // 回传并推送offer
  165. type:2
  166. },
  167. });
  168. }).catch(error => {
  169. });
  170. peer.oniceconnectionstatechange = function (ev) {
  171. if(peer.connectionState=="closed"){
  172. markNote.innerHTML=state5;
  173. }
  174. if(peer.connectionState=="connected"){
  175. markNote.innerHTML=state3;
  176. }
  177. if(peer.connectionState=="connecting"){
  178. markNote.innerHTML=state2;
  179. }
  180. if(peer.connectionState=="disconnected"){
  181. markNote.innerHTML=state4;
  182. }
  183. };
  184. localCandidata=[]
  185. peer.onicecandidate = function (event) {
  186. if (event.candidate){
  187. localCandidata.push(event.candidate);
  188. }
  189. }
  190. startCommunicate();
  191. }
  192. async function RCremoteData(rcData){//收到remoteAnswer
  193. //记录远端offer
  194. await peer.setRemoteDescription(rcData)
  195. uni.postMessage({
  196. data: {
  197. data:localCandidata, // 发起方先发candidate
  198. type:3
  199. },
  200. });
  201. }
  202. function startCommunicate(){
  203. peer.ontrack = (e) => {
  204. if(e.streams[0]){
  205. var otherVideos = document.querySelector('#my_audio');
  206. //var otherVideos = document.querySelector('#Big_video');
  207. otherVideos.srcObject = e.streams[0];
  208. }
  209. // sm_video.srcObject =localStream;
  210. // sm_video.setAttribute('autoplay', true); /* THIS */
  211. }
  212. }
  213. async function RCstartWebRct(payload){
  214. peer = new RTCPeerConnection(
  215. {
  216. iceServers:[
  217. {
  218. urls:'stun:stun.l.google.com:19302',
  219. }
  220. ]
  221. }
  222. );
  223. //添加本地音视频
  224. localStream.getTracks().forEach((track) => {
  225. peer.addTrack(track, localStream)
  226. })
  227. localCandidata=[];
  228. peer.onicecandidate = function (event) {
  229. if (event.candidate){
  230. //let strcandidate=JSON.stringify(event.candidate);
  231. localCandidata.push(event.candidate);
  232. }
  233. }
  234. startCommunicate();
  235. //记录远端offer
  236. await peer.setRemoteDescription(payload)
  237. const remoteAnswer = await peer.createAnswer();
  238. //记录本地offer
  239. await peer.setLocalDescription(remoteAnswer);
  240. //推送remoteAnswer
  241. uni.postMessage({
  242. data: {
  243. data:remoteAnswer, // 回传并推送offer
  244. type:2
  245. },
  246. });
  247. }
  248. function initRtc() {
  249. if(language=='yuenan'){//越南语
  250. ICnote='美食達 Rider yêu cầu IM Voice Call';
  251. title='IM Cuộc gọi thoại';
  252. state1='Đang gọi';
  253. state2='Đang kết nối';
  254. state3='Đã kết nối';
  255. state4='Bị treo';
  256. state5='Kết thúc cuộc gọi';
  257. }
  258. if(language=='zh-Hans'){//简体中文
  259. ICnote='美食達骑手请求IM语音通话'
  260. title='IM 语音通话';
  261. state1='正在呼叫';
  262. state2='正在接通';
  263. state3='已接通';
  264. state4='已挂断';
  265. state5='结束通话';
  266. }
  267. if(language=='zh-Hant'){//繁体中文
  268. ICnote='美食達騎手請求IM語音通話'
  269. title='IM 語音通話';
  270. state1='正在呼叫';
  271. state2='正在接通';
  272. state3='已接通';
  273. state4='已掛斷';
  274. state5='結束通話';
  275. }
  276. markNote.innerHTML=title;
  277. setTimeout(() => {
  278. //startCapture({ video: true, audio: true});
  279. startCapture({audio: true});
  280. }, 1000)
  281. return;
  282. }
  283. function releaseTack(){
  284. if(peer){
  285. peer.close();
  286. }
  287. }
  288. window.onload=initRtc;
  289. window.onunload=releaseTack;
  290. </script>
  291. </html>