简体   繁体   English

不按鼠标或 Javascript 中的任何键,音频无法播放

[英]Audio is not playing without pressing mouse or any key in Javascript

I am trying to play an audio which is a smoke detecting sound whenever I sense smoke.每当我感觉到烟雾时,我都会尝试播放一种烟雾检测声音。 But the problem is audio is not playing correctly.但问题是音频播放不正确。 If I click the mouse on the page, then sound comes.如果我在页面上单击鼠标,就会出现声音。 Even if I press any key from the keyboard, then also sound comes.即使我按下键盘上的任何键,也会发出声音。 So, I need a way to play the music without clicking the mouse on the page or pressing any key from the keyboard.因此,我需要一种无需在页面上单击鼠标或按键盘上的任何键即可播放音乐的方法。 Help me out.帮帮我。 Thanks.谢谢。

<tr>
    <table align="center" style=" border: 5px solid grey; width:100%;">
       <td valign="center"><t style="font-size: 70px; padding-left:0px; color: white; font-family: serif;">SMOKE:</t></td>
       <td valign="center">
          <i id="smoke" style="font-family: 'verdana';  font-weight: 'bold'; font-size: 75px;">{{ smokestatus }}</i> 
       </td>
       </tr>
       <audio id="myAudio">
          <source src={{ url_for('static', filename='smoke_detector_beeps.mp3') }} type="audio/mpeg">
          Your browser does not support the audio element.
       </audio>
       <script>
          var x = document.getElementById("myAudio"); 
          function playAudio() { 
            x.play(); 
          } 
          function pauseAudio() { 
            x.pause(); 
          } 
       </script>
       
       <script>
          if (document.getElementById('smoke').innerText == "NO SMOKE" ) {
          document.getElementById('smoke').style.color = 'green';
          }
          else if (document.getElementById('smoke').innerText == "SMOKE DETECTED" ) {
          document.getElementById('smoke').style.color = 'red';
          document.getElementById('smoke').style.fontSize = '65px';
          document.getElementById("smoke").className = "blink"; 
          playAudio();
          }
          else if (document.getElementById('smoke').innerText == "SENSOR DISCONNECTED" ) {
          document.getElementById('smoke').style.color = 'yellow';
          document.getElementById('smoke').style.fontSize = '60px';
          pauseAudio();
          }
          else if (document.getElementById('smoke').innerText == "SENSOR CONNECTED" ) {
          document.getElementById('smoke').style.color = 'green';
          }
          else {
          document.getElementById('smoke').style.color = 'white';
          }
       </script>
    </table>
 </tr>

The answer depends on when you want the sound to be played:答案取决于您希望何时播放声音:

If you only want to check for the smoke status on page load, you can wrap your logic that's currently within the second script tag into a function.如果您只想检查页面加载时的烟雾状态,您可以将当前位于第二个脚本标记中的逻辑包装到 function 中。

This function can then be called when the window loads by using:然后可以在 window 加载时调用此 function,方法是使用:

window.onload = checkSmoke()

If you want the function to be called multiple times whenever the smoke status changes, you will have to call the function again.如果您希望在烟雾状态发生变化时多次调用 function,则必须再次调用 function。

Here's a small JSFiddle to show what I mean.这是一个小的JSFiddle来说明我的意思。

If this wasn't the answer you were looking for, let me know!如果这不是您要找的答案,请告诉我!

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

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