如何使用 Javascript 禁用 webrtc 麦克风?

How to disable the webrtc microphone using Javascript?

大家好,我正在使用 Peer Js 创建一个 WebRtc,它就像一个视频通话,只是我想创建一个按钮,当有人点击它时,关闭麦克风,这段代码在 script.js:

const PRE = "DELTA";
const SUF = "MEET";
var room_id;
var getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
var local_stream;

function createRoom(){
    console.log("Creating Room")
    let room = document.getElementById("room-input").value;
    if(room == " " || room == "")   {
        alert("Please enter room number")
        return;
    }
    room_id = PRE+room+SUF;
    let peer = new Peer(room_id)
    peer.on('open', (id)=>{
        console.log("Peer Connected with ID: ", id)
        hideModal()
        getUserMedia({video: true, audio: true}, (stream)=>{
            local_stream = stream;
            setLocalStream(local_stream)
        },(err)=>{
            console.log(err)
        })
        notify("Waiting for peer to join.")
    })
    peer.on('call',(call)=>{
        call.answer(local_stream);
        call.on('stream',(stream)=>{
            setRemoteStream(stream)
        })
    })
}

function setLocalStream(stream){
    let video = document.getElementById("local-video");
    video.srcObject = stream;
    video.muted = true;
    video.play();
}
function setRemoteStream(stream){
   
    let video = document.getElementById("remote-video");
    video.srcObject = stream;
    video.play();
}

function hideModal(){
    document.getElementById("entry-modal").hidden = true
}

function notify(msg){
    let notification = document.getElementById("notification")
    notification.innerHTML = msg
    notification.hidden = false
    setTimeout(()=>{
        notification.hidden = true;
    }, 3000)
}

function joinRoom(){
    console.log("Joining Room")
    let room = document.getElementById("room-input").value;
    if(room == " " || room == "")   {
        alert("Please enter room number")
        return;
    }
    room_id = PRE+room+SUF;
    hideModal()
    let peer = new Peer()
    peer.on('open', (id)=>{
        console.log("Connected with Id: "+id)
        getUserMedia({video: true, audio: true}, (stream)=>{
            local_stream = stream;
            setLocalStream(local_stream)
            notify("Joining peer")
            let call = peer.call(room_id, stream)
            call.on('stream', (stream)=>{
                setRemoteStream(stream);
            })
        }, (err)=>{
            console.log(err)
        })

    })
}

这是 Html 代码:

<div class="entry-modal" id="entry-modal">
        <p>Create or Join Meeting</p>
        <input id="room-input" class="room-input" placeholder="Enter Room ID">
        <div style="display:inline-block;"><br>
            <button onclick="createRoom()" class="create_room">Create room</button><br><br>
            <button onclick="joinRoom()" class="join_room">Join Room</button><br>
        </div>
</div>
<br><br><br>
<div class="meet-area">
    <!-- Remote Video Element-->
    <video id="remote-video"></video>

    <!-- Local Video Element-->
    <video id="local-video"></video>
</div>

所以我创建了这个按钮:

<button onclick="off()">Off Microphone</button>

那么如何制作能够禁用麦克风的脚本?

我没有对此进行测试,但您可以像这样将音频静音

const off=function(){//toggle state
    local_stream.getAudioTracks()[0].enabled = !local_stream.getAudioTracks()[0].enabled;
};

const off=function(){
    local_stream.getAudioTracks()[0].enabled = false;
};
const on=function(){
    local_stream.getAudioTracks()[0].enabled = true;
};