| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <link rel="stylesheet" type="text/css" href="./css/index.css" />
- <title>语音通话</title>
- <style>
- html, body {
- height: 100%;
- }
- body {
- margin: 0;
- }
- </style>
- </head>
- <body>
- <audio id="my_audio" autoplay></audio>
- <div class="contentColumnC" style="width: 100%;margin-top: 60px;"">
-
- <img id="Avatar" class="iconImg" style="margin-top: 5px;" src="./img/logo.png" alt=""/>
- <div class="contentInRowC" style="width: 100%;margin-top: 10px;">
- <div id="Name"></div>
- </div>
- </div>
- <div class="contentInRowC" style="width: 100%;margin-top: 40px;">
- <div id="Note"></div>
- <img class="calling" src="./img/loading2.gif" alt="" />
- </div>
- <div style="width: 100%;margin-top: 100px;">
- <div class="contentColumnC">
- <img class="guadanImg" style="margin-top: 5px;" src="./img/icon_off.png" alt="" onclick="guaduan()"/>
- </div>
- </div>
- </body>
- <script type="text/javascript" src="./js/lib/uniwebviewsdk.js"></script>
- <script>
- let localStream = null;
- let localCandidata=null;
- let peer = null;
- let state1,state2,state3,state4,state5;
- const markNote = document.getElementById('Note');
-
- //====================================================
- function getQueryVariable(variable) {
- var query = decodeURI(window.location.search.substring(1));
- var vars = query.split("&");
- for (var i = 0; i < vars.length; i++) {
- var pair = vars[i].split("=");
- if (pair[0] == variable) {
- return pair[1];
- }
- }
- return (false);
- }
-
- function guaduan(){
- if(peer){
- peer.close();
- }
- uni.postMessage({
- data: {
- data:'',
- type:'close'
- },
- });
- }
- //====================================================
- function appAct (obj) {
- //发起方收到接受方 sdp answer
- if(obj.type=='answer'){
- //markNote.innerHTML='收到answer';
- RCremoteData(obj);
- }
- }
- function appActcandidate (obj){//发起方收到candidate 添加到Rct
- uni.postMessage({
- data: {
- data:obj, //应答方回应candidate
- type:'1111'
- },
- });
- for(var i=0;i<obj.length;i++){
- peer.addIceCandidate(obj[i]);
- }
- }
- async function startCapture(displayMediaOptions) {
- try {
- localStream = await navigator.mediaDevices.getUserMedia(displayMediaOptions);
- //startWebRct()
- } catch(err) {
- console.error(err);
- }
- }
- async function startWebRct(){
- peer = new RTCPeerConnection(
- {
- iceServers:[
- {
- urls: ["stun:203.175.169.52:3478",
- "turn:203.175.169.52:3478"
- ],
- username: 'aaaaa',
- credential: 'bbbbb'
- }
- ]
- }
- );
- //添加本地音视频
- localStream.getTracks().forEach((track) => {
- peer.addTrack(track, localStream)
- });
- //创建本地offer
- const offer = peer.createOffer({
- offerToReceiveAudio:1,
- offerToReceiveVideo:1
- });
- //记录本地offer
- offer.then(value => {
- peer.setLocalDescription(value);
- uni.postMessage({
- data: {
- data:value, // 回传并推送offer
- type:'offer'
- },
- });
- }).catch(error => {
- });
- peer.onconnectionstatechange = function (ev) {
- if(peer.connectionState=="closed"){
- markNote.innerHTML=state5;
- }
- if(peer.connectionState=="connected"){
- markNote.innerHTML=state3;
- uni.postMessage({
- data: {
- data:'',
- type:"connect"
- },
- });
- }
- if(peer.connectionState=="connecting"){
- markNote.innerHTML=state2;
- }
- if(peer.connectionState=="disconnected"){
- markNote.innerHTML=state4;
- }
- };
- localCandidata=[]
- peer.onicecandidate = function (event) {
- if (event.candidate){
- localCandidata.push(event.candidate);
- }
- }
- startCommunicate();
- }
- //发起方收到 远端SDP answer 保存并发送candidate
- async function RCremoteData(rcData){//收到remoteAnswer
- //记录远端offer
- await peer.setRemoteDescription(rcData)
- // 发起方先发回candidate
- uni.postMessage({
- data: {
- data:localCandidata,
- type:"candidate"
- },
- });
- }
- function startCommunicate(){
- peer.ontrack = (e) => {
- if(e.streams[0]){
- var otherVideos = document.querySelector('#my_audio');
- otherVideos.srcObject = e.streams[0];
- }
- }
- }
-
- function initRtc() {
- state1='正在呼叫';
- state2='正在接通';
- state3='已接通';
- state4='已挂断';
- state5='结束通话';
- document.getElementById('Name').innerHTML=getQueryVariable('friendName');;
- document.getElementById('Avatar').src=getQueryVariable('friendAvatar');;
- markNote.innerHTML=state1;
- setTimeout(() => {
- //startCapture({ video: true, audio: true});
- startCapture({ video: false, audio: true});
- }, 1000)
- return;
- }
- function releaseTack(){
- if(peer){
- peer.close();
- }
- }
- window.onload=initRtc;
- window.onunload=releaseTack;
- </script>
-
- </html>
|