首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >停止AudioElement onClick

停止AudioElement onClick
EN

Stack Overflow用户
提问于 2022-01-08 08:23:14
回答 1查看 31关注 0票数 0

希望你能帮我。我现在困在这个问题上好几天了,解决不了。使用BTNReminder按钮,我设置了一个定时器。当时机成熟时,AudioElement播放我设定的秒。现在我试图阻止声音,但我不能赶上AudioElement。我试过把整张表都弄掉了,但那也没什么用。理想上,我想做一些功能,把音频元素按一下,就像这个audioElement.pause();

这个是可能的吗?我犯了什么错误吗?

求助请求:-/

代码语言:javascript
复制
<!DOCTYPE html>
<html lang="de">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <title>Major Support</title>
    <script
      src="http://code.jquery.com/jquery-latest.min.js"
      type="text/javascript"
    ></script>

  </head>


      <div class="buttonWrapperDiv">
        <h3 id="minutes">00:00</h3>
        <span id="count"></span>
        <button id="stopBTNtwo">STOP</button>
      </div>
      <div class="btnWrapper">
        <button id="BTNReminder" value="5">5 seconds</button>
      </div>
    </div>
代码语言:javascript
复制
    <script>

$(function () {
        function countdownReal(number) {

          $("#").html(number);
          if (number === 0) {
            d.resolve();
          } else {
            number -= 1;
            window.setTimeout(function () {
              countdownReal(number);
            }, 1000);
          }
          return d.promise();
        }

        function waitForAudioToFinish(audioElement) {
          if (!audioElement.paused) {
            setTimeout(function () {
              waitForAudioToFinish(audioElement);
            }, 200);
          } else {
            d1.resolve();
          }
        }
    
        function playSong(src) {
          var audioElement = document.createElement("audio");
              audioElement.setAttribute("autoplay", "autoplay");
          audioElement.setAttribute("id", "beepbeep");
          audioElement.setAttribute("value", "test");
          audioElement.setAttribute("src", src);
          audioElement.play();
       
          waitForAudioToFinish(audioElement);
     

          function mute(audioElement){
        audioElement.muted = true;
        audioElement.pause();
        }

          return d1.promise();
        
        }
   

        var d = new $.Deferred();
        var d1 = new $.Deferred();

        $("#BTNReminder").click(function () {
          var fired_button = $(this).val();


          $("#BTNReminder").on("click", function () {
            function countdown(fired_button) {
              var seconds = 5;
              var mins = 1;
              function tick() {
                var minutenCounter = document.getElementById("minutes");
                var current_minutes = mins - 1;
                seconds--;
                minutenCounter.innerHTML =
                  current_minutes.toString() +
                  ":" +
                  (seconds < 10 ? "0" : "") +
                  String(seconds);
                if (seconds > 0) {
                  setTimeout(tick, 1000);
                } else {
                  if (mins > 1) {
                    countdown(mins - 1);
                  }
                }
              }

              tick();
            }
            countdown(fired_button);


            function countdownZwei(fired_button) {
              var seconds = 5;
              var mins = 1;

              function tick() {
                var minutenCounter = document.getElementById("minutes");
                var current_minutes = mins - 1;
                seconds--;
                minutenCounter.innerHTML =
                  current_minutes.toString() +
                  ":" +
                  (seconds < 10 ? "0" : "") +
                  String(seconds);
                if (seconds > 0) {
                  setTimeout(tick, 1000);
                } else {
                  if (mins > 1) {
                    countdown(mins - 1);
                  }
                }
              }

              tick();
            }
            countdownZwei(fired_button);
            $(this).prop("disabled", true);
            $.when(countdownReal(fired_button)).then(function () {


              playSong("audio.mp3").then(function () {
                $(".BTNmajor").prop("disabled", false);
              });
            });
          });
        });
      });
    

    </script>

  </body>
</html>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-09 16:22:12

我用这种方法解决了这个问题:

代码语言:javascript
复制
var audioElement = document.createElement("audio");
       
function playSong(src) {
     
            audioElement.setAttribute("autoplay", "autoplay");
            audioElement.setAttribute("src", src);
            audioElement.play();
            waitForAudioToFinish(audioElement);
            return d1.promise();
        
        }
   
   
$("#stopBTN").click(function () {

audioElement.pause()
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70630697

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档