2017-08-08 68 views
0

我已經在我的WebRTC視頻聊天頁面中插入了一個靜音按鈕,但是我無法使其工作。如果我在瀏覽器中點擊它,我會得到一個控制檯消息,聲音已被靜音,但仍然有聲音。WebRTC:聲音不靜音 - 任何人都可以看到一個錯誤?

的約束變量:

var constraints = { 
    video: true, 
    audio: true, 
}; 

如果我改變音頻假這裏不會有任何聲音。

守則靜音按鈕:

function muteVideoBtnClick() { 

if(constraints.audio == true) { 
    constraints.audio = false; 
    console.log('Audio: ' + constraints.audio); 
} else { 
    constraints.audio = true; 
    console.log('Audio: ' + constraints.audio); 
} 

}

當使用約束變量的唯一其他地方:

function pageReady() { 
uuid = uuid(); //CB Universal Unique Identifier 


//CB Create the variables for local and remote video 
localVideo = document.getElementById('localVideo'); 
remoteVideo = document.getElementById('remoteVideo'); 
//CB Create the connection using websocket (443 as it is a secure connection) 
serverConnection = new WebSocket('wss://' + window.location.hostname + ':443'); 
serverConnection.onmessage = gotMessageFromServer; 


// CB Checks thats getUserMedia works and then runs getUserMedia if it works and displays an error 
//if it doesnt work 
if(navigator.mediaDevices.getUserMedia) { 
    navigator.mediaDevices.getUserMedia(constraints).then(getUserMediaSuccess).catch(errorHandler); 

} else { 
    alert('Your browser does not support getUserMedia API'); 
} 

}

如果有人有任何建議,我將非常感激。

親切的問候, 克萊爾

的完整代碼:

var localVideo; 
    var remoteVideo; 
    var peerConnection; 
    var uuid; 
    var rooms = [];//CB 31/07 
    var constraints = { 
     video: true, 
     audio: true, 
    }; 

    var peerConnectionConfig = { 
     'iceServers': [ 
      {'urls': 'stun:stun.services.mozilla.com'}, 
      {'urls': 'stun:stun.l.google.com:19302'}, 
     ] 
    }; 


    function pageReady() { 
    uuid = uuid(); //CB Universal Unique Identifier 


    //CB Create the variables for local and remote video 
    localVideo = document.getElementById('localVideo'); 
    remoteVideo = document.getElementById('remoteVideo'); 
    //CB Create the connection using websocket (443 as it is a secure connection) 
    serverConnection = new WebSocket('wss://' + window.location.hostname + ':443'); 
    serverConnection.onmessage = gotMessageFromServer; 


    // CB Checks thats getUserMedia works and then runs getUserMedia if it works and displays an error 
    //if it doesnt work 
    if(navigator.mediaDevices.getUserMedia) { 
     navigator.mediaDevices.getUserMedia(constraints).then(getUserMediaSuccess).catch(errorHandler); 

    } else { 
     alert('Your browser does not support getUserMedia API'); 
    } 
} 

//CB if it is possible to run gerUserMedia then gets the local video stream 
function getUserMediaSuccess(stream) { 
    localStream = stream; 
    localVideo.src = window.URL.createObjectURL(stream); //Depreciated!!!!! 
    //localVideo.srcObject = stream; 
} 


//CB this function starts the call 
function start(isCaller) { 
    peerConnection = new RTCPeerConnection(peerConnectionConfig); 
    peerConnection.onicecandidate = gotIceCandidate; 
    peerConnection.onaddstream = gotRemoteStream; 
    //peerConnection.ontrack = gotRemoteStream; 
    peerConnection.addStream(localStream); 

    if(isCaller) { 
     peerConnection.createOffer().then(createdDescription).catch(errorHandler); 
    } 
} 


//Added by CB for Pause Button 20/07 
function pauseVideoBtnClick() { 
    var btn = document.getElementById("pause_video_btn"); 
    if (isVideoPaused()) { 
     pauseVideo(false); 
      btn.innerHTML = "Pause Video"; 
     } else { 
     pauseVideo(true); 
      btn.innerHTML = "Resume Video"; 
     } 
} 

//Added by CB for Pause Button 20/07 
function isVideoPaused() { 
    return !(localStream.getVideoTracks()[0].enabled); 
} 

//Added by CB for Pause Button 20/07 
function pauseVideo (pause) { 
    localStream.getVideoTracks()[0].enabled = !pause; 
}; 

//Added by CB for mute button 29/07 - DOESNT WORK YET 
function muteVideoBtnClick() { 

    if(constraints.audio == true) { 
     constraints.audio = false; 
     console.log('Audio: ' + constraints.audio); 
    } else { 
     constraints.audio = true; 
     console.log('Audio: ' + constraints.audio); 
    } 
} 

//End of added code 

function gotMessageFromServer(message) { 
    if(!peerConnection) start(false); 

    var signal = JSON.parse(message.data); 

    // Ignore messages from ourself 
    if(signal.uuid == uuid) return; 

    if(signal.sdp) { 
     peerConnection.setRemoteDescription(new RTCSessionDescription(signal.sdp)).then(function() { 
      // Only create answers in response to offers 
      if(signal.sdp.type == 'offer') { 
       peerConnection.createAnswer().then(createdDescription).catch(errorHandler); 
      } 
     }).catch(errorHandler); 
    } else if(signal.ice) { 
     peerConnection.addIceCandidate(new RTCIceCandidate(signal.ice)).catch(errorHandler); 
    } 
} 

function gotIceCandidate(event) { 
    if(event.candidate != null) { 
     serverConnection.send(JSON.stringify({'ice': event.candidate, 'uuid': uuid})); 
    } 
} 

function createdDescription(description) { 
    console.log('got description'); 

    peerConnection.setLocalDescription(description).then(function() { 
     serverConnection.send(JSON.stringify({'sdp': peerConnection.localDescription, 'uuid': uuid})); 
    }).catch(errorHandler); 
} 

function gotRemoteStream(event) { 
    console.log('got remote stream'); 
    remoteVideo.src = window.URL.createObjectURL(event.stream); 
    //remoteVideo.src = event.stream; 
} 

function errorHandler(error) { 
    console.log(error); 
} 


    // CB A UUID (Universal Unique Identifier) is a 128-bit number used to uniquely identify some object or entity on the Internet. 
    // Taken from http://stackoverflow.com/a/105074/515584 
    // Strictly speaking, it's not a real UUID, but it gets the job done here 
    function uuid() { 
     function s4() { 
     return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1); 
    } 

    return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4(); 
} 

回答

1

您正在改變約束的getUserMedia調用(後做調用)。您不會更改存儲在localStream變量中的結果流。試試這個: localStream.getAudioTracks()[0].enabled = false;

+0

非常感謝菲利普,我雖然已經嘗試過這一點,顯然沒有放棄我的想法!真的很感謝幫助! – clairebear8182

相關問題