简体   繁体   English

使用javascript切换静音和取消静音

[英]toggle mute and unmute with javascript

I'm trying to create a button for muting and un-mute. 我正在尝试创建一个用于静音和取消静音的按钮。

Initially I have by button set up like this: 最初,我通过如下方式设置了按钮:

<i class="fa fa-microphone" id="audio"></i>

and what I want is that when you click it, the class becomes: 我想要的是,当您单击它时,该类变为:

<i class="fa fa-microphone-slash" id="audio"></i>

now I have my javascript set up like this: 现在,我的JavaScript设置如下:

$(document).ready(function() {
  $('#audio').click(function() {
    publisher.publishAudio(false);
  });
});

which essentially mutes it, now I need to make it so that it can also unmute when I click on the button 基本上将其静音,现在我需要制作它,以便在单击按钮时也可以取消静音

Using jQuery, I'd chain toggleClass , along with an if statement using hasClass for the logic: 使用jQuery,我hasClass toggleClass以及一个使用hasClass作为逻辑的if语句hasClass

$(document).ready(function() {
  $('#audio').click(function() {
    if ($(this).hasClass("fa-microphone-slash")) {
        publisher.publishAudio(true);
    } else {
        publisher.publishAudio(false);
    }
    $(this).toggleClass("fa-microphone-slash");
    $(this).toggleClass("fa-microphone");

    publisher.publishAudio($(this).hasClass(""));
  });
});

If you want a DRY solution with no if statement, based on the class: 如果要基于类的DRY解决方案没有if语句,请执行以下操作:

$(document).ready(function() {
  $('#audio').click(function() {
    publisher.publishAudio($(this).hasClass("fa-microphone-slash"));    
    $(this).toggleClass("fa-microphone-slash");
    $(this).toggleClass("fa-microphone");
  });
});

The second solution will just use the presence of the fa-microphone tag as a boolean for whether to perform a mute. 第二种解决方案将只使用fa-microphone标签作为是否执行静音的布尔值。

I don't know jquery that well but here's some simple javascript code to manipulate the class 我不太了解jquery,但这是一些简单的javascript代码来操纵该类

$(document).ready(function() {
    $('#audio').click(function() {
        var audio = document.querySelector("#audio");

        // get current class state
        var currentState = audio.getAttribute("class");

        // change class
        if(currentState === "fa fa-microphone"){
           audio.setAttribute("class", "fa fa-microphone-slash");
        }else{
           audio.setAttribute("class", "fa fa-microphone");
        }

    });
});

you can check if the publisher is sending video/audio on the stream it self like this: 您可以检查发布者是否在自己的流上发送视频/音频,如下所示:

publisher.stream.hasAudio
publisher.stream.hasVideo

then you can use the ! 那么您可以使用! (bang) to invert it (砰)将其反转

publisher.publishAudio(!publisher.stream.hasAudio);
publisher.publishVideo(!publisher.stream.hasVideo);

so to toggle the class aswell you do someting like 所以也要切换班级

$(document).ready(function() {
    var toggleAudioBtn = $('#audio')
    toggleAudioBtn.click(function() {
       toggleAudioBtn.toggleClass("fa-microphone-slash").toggleClass("fa-microphone");
       publisher.publishAudio(!publisher.stream.hasAudio); 
    });
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM