当我按下 b 键时,我试图播放声音,但我似乎无法弄清楚
HTML:
<audio id="music">
<source src="asap.mp3" type="audio/mpeg">
</audio>
JS:
var s2 = document.getElementById('music')
document).keydown(function(e){
if (e.keyCode == 66) {
s2.play();
}
});
答案 0 :(得分:2)
//to play on the key A do this(using Jquery):
document.addEventListener('keydown', function(e) {
if(e.keyCode == 66){
document.getElementById('audio').play();
}
if(e.keyCode == 65){
document.getElementById('audio').pause();
}
});
<p>
press the "b" to play
</p>
<p>
press the "a" to pause
</p>
<!-- Create an audio element and hide it with css: display:none -->
<audio id="audio" controls style="display:none">
<source src="http://butlerccwebdev.net/support/html5-video/media/soundfile.mp3" type="audio/mpeg">
Your browser does not support the audio element.
</audio>