WebRTC:声音没有静音 - 任何人都可以看到错误吗?

clairebear8182

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

}

如果有人有任何建议,我将不胜感激。

亲切的问候,克莱尔

完整代码:

  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;

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

任何人都可以帮助我使这个 java 运行没有错误吗?

任何人都可以解释这种有关数组排序的奇怪行为吗?

任何人都可以在没有密钥的情况下解码JSON Web令牌(JWT)吗?

输出:没有错误您可能分配了太多内存 任何人都可以详细解释

任何人都可以清楚地说明为什么Google Guice有用吗?

任何人都可以提出解决此类任务的想法,而无需遍历所有子集吗?

任何人都可以在 Google 表格中查看有关 ArrayFormula 的这个公式吗?

任何人都可以提供有关如何使用Windows ShutdownBlockReasonCreate的示例

拥有共享Google链接的任何人都可以在没有Google帐户的情况下将文件添加到该文件夹吗?

当有多个本地化属性时,任何人都可以帮助我解决主动事件 API 中本地化属性数组的结构吗?

任何人都可以告诉我有关网络托管和 filezilla 主机用户名和密码的区别吗?

任何人都可以提供有关monkeypatching/hooking Ruby 字符串#{} 以进行插值的指导吗?

线程用户 2000 出现 503 错误。任何人都可以帮助我这里有什么问题

任何人都可以解释为什么 mysql 没有按预期使用索引

任何人都可以建议我是否有任何可能的方法来在CSS中添加php变量

如果任何人都可以创建和上传任何密钥,那么maven的OpenPGP签名有什么意义?

任何人都可以解释android中的未绑定和绑定服务有什么区别

任何人都可以建议与抖动图像缩放有关的软件包

任何人都可以帮助我有关SQL查询的问题(在哪里)

我不斷收到錯誤“字符串索引超出範圍”我已確保數組具有足夠的索引。任何人都可以看到問題嗎?

任何人都可以提出一种简单的方法来使用没有 DataAdapter 的 Microsoft.Data.Sqlite 更新带有更改数据的 SQLite 数据库吗?

任何人都知道有一个程序可以在摘下耳机后使声音静音吗?

Ubuntu:如果任何人都可以在没有密码的情况下得到root提示,这是否损害了gnome密钥环?

当我收到错误错误代码:1248 时,我正在这样做。每个派生表都必须有自己的别名,任何人都可以帮助我,为什么会出错?

任何人都可以更正此代码吗?

任何人都可以理解connStateInterface吗?

任何人都可以帮助解析HCL吗?

任何人都可以处理吗?

任何人都可以回答这个问题吗?