HTML音频在按下特定键时播放问题

huangapple go评论67阅读模式
英文:

HTML audio playing issues when specific key is pressed

问题

I'm trying to make this audio play when a specific key is pressed and will continue to loop. Any ideas on how to make this work?

document.addEventListener("keypress", function(event) {
  if (event.keyCode == 13) {
    <audio controls loop>
      <source src="Chiptronical.ogg" type="audio/ogg">
    </audio>
  }
});

I wanted it to play the audio when the key is pressed, but it just doesn't work.

英文:

Im trying to make this audio play when a specific key is pressed and will continue to loop. Any ideas on how to make this work?

document.addEventListener(&quot;keypress&quot;, function(event) {
  if (event.keyCode == 13) {
&lt;audio controls loop&gt;
    &lt;source src=&quot;Chiptronical.ogg&quot; type=&quot;audio/ogg&quot;&gt;
    &lt;/audio&gt;
  }
});

i wanted it to play the audio when the key is pressed but it just doesnt work

答案1

得分: 0

以下是您提供的HTML代码的翻译部分:

尝试在浏览器上运行这个代码,并按回车键。
确保目录与音频文件相同。

<!DOCTYPE html>
<html>
<head>
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script>
    $(document).ready(function(){
    
    document.addEventListener("keypress", function(event) {
  if (event.keyCode == 13) {
    
    	$('body').append( '<audio controls autoplay>' +
  '<source src="Chiptronical.ogg" type="audio/ogg">' +
  '<source src="Chiptronical.mp3" type="audio/mpeg">' +
  'Your browser does not support the audio element.' +
'</audio>' );
 }
});


    });
    </script>
</head>

<body>

<h1>音频自动播放属性</h1>

<p>按回车键播放声音:</p>

</body>
</html>

请注意,这是您提供的HTML代码的翻译部分,其中包含HTML标记和JavaScript代码。

英文:

try this and run on browser. and press enter.
make sure directory is same with audio file

&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;script src=&quot;https://code.jquery.com/jquery-1.12.4.js&quot;&gt;&lt;/script&gt;
    &lt;script&gt;
    $(document).ready(function(){
    
    document.addEventListener(&quot;keypress&quot;, function(event) {
  if (event.keyCode == 13) {
    
    	$(&#39;body&#39;).append( `&lt;audio controls autoplay&gt;
  &lt;source src=&quot;Chiptronical.ogg&quot; type=&quot;audio/ogg&quot;&gt;
  &lt;source src=&quot;Chiptronical.mp3&quot; type=&quot;audio/mpeg&quot;&gt;
  Your browser does not support the audio element.
&lt;/audio&gt;` );
 }
});


    });
    &lt;/script&gt;
&lt;/head&gt;

&lt;body&gt;

&lt;h1&gt;The audio autoplay attribute&lt;/h1&gt;

&lt;p&gt;Press Enter to play a sound:&lt;/p&gt;

&lt;/body&gt;
&lt;/html&gt;

答案2

得分: 0

以下是翻译好的部分:

你可以像这样实现所需的结果:-

这是HTML代码:-

<audio id="audioElement" controls loop>
    <source src="Chiptronical.ogg" type="audio/ogg">
</audio>

这是JavaScript代码:-

const audioElement = document.querySelector('#audioElement');
document.addEventListener("keypress", function(event) {
  if (event.keyCode == 13) {
      // 播放
      audioElement.play();
      // 暂停
      audioElement.pause();
  }
});
英文:

You can achieve the desired results like this:-

Here is the html:-

&lt;audio id=&quot;audioElement&quot; controls loop&gt;
    &lt;source src=&quot;Chiptronical.ogg&quot; type=&quot;audio/ogg&quot;&gt;
&lt;/audio&gt;

Here is the JavaScript:-

const audioElement = document.querySelector(&#39;#audioElement&#39;);
document.addEventListener(&quot;keypress&quot;, function(event) {
  if (event.keyCode == 13) {
      // For Play
      audioElement.play();
      // For Pause
      audioElement.pause();

  }
});

huangapple
  • 本文由 发表于 2023年5月11日 18:10:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76226478.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定