Audio WebRTC:声音未静音-有人能看到错误吗?

Audio WebRTC:声音未静音-有人能看到错误吗?,audio,webrtc,Audio,Webrtc,我已经在我的WebRTC视频聊天页面中插入了一个静音按钮,但无法使其工作。如果我在浏览器中单击它,我会收到一条控制台消息,声音已被静音,但仍有声音 约束变量: var constraints = { video: true, audio: true, }; 如果我将音频更改为false,这里将没有声音 静音按钮上的代码: function muteVideoBtnClick() { if(constraints.audio == true) { constraints

我已经在我的WebRTC视频聊天页面中插入了一个静音按钮,但无法使其工作。如果我在浏览器中单击它,我会收到一条控制台消息,声音已被静音,但仍有声音

约束变量:

var constraints = {
    video: true,
    audio: true,
};
如果我将音频更改为false,这里将没有声音

静音按钮上的代码:

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');
}
}

使用约束变量的唯一其他位置:

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();
}

您正在更改getUserMedia调用的约束(在执行调用之后)。您没有更改存储在localStream变量中的结果流。试试这个:
localStream.getAudioTracks()[0]。enabled=false

您正在更改对getUserMedia调用的约束(在调用之后)。您没有更改存储在localStream变量中的结果流。试试这个:
localStream.getAudioTracks()[0]。enabled=false

非常感谢你,菲利普,我以为我已经试过了,显然是疯了!!非常感谢你的帮助!非常感谢你,菲利普,我想我已经试过了,显然是疯了!!非常感谢你的帮助!