javascript – 按下某个键时播放声音
栏目: JavaScript · 发布时间: 6年前
内容简介:翻译自:https://stackoverflow.com/questions/12578379/play-a-sound-when-a-key-is-pressed
它很容易播放声音,并且很容易为按键添加处理程序,但是没有预定义的方法来链接这两个操作,因此您必须键入自己的代码.
1)按键操作
document.onkeydown = function() {
...
2)播放声音
添加音频元素:
<audio id=alarm>
<source src=sound/zbluejay.wav>
</audio>
并执行它
document.getElementById('alarm').play();
例如,您可以构建一个将键码链接到声音元素ID的地图:
var sounds = {
88 : 'alarm', // key 'x'
...
};
document.onkeydown = function(e) {
var soundId = sounds[e.keyCode];
if (soundId) document.getElementById(soundId).play();
else console.log("key not mapped : code is", e.keyCode);
}
Yoy可能会找到密钥代码 here
翻译自:https://stackoverflow.com/questions/12578379/play-a-sound-when-a-key-is-pressed
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- 通过WinAPI播放PCM声音
- Chrome 66 禁止声音自动播放,开发怎么应对?
- Chrome 下次更新将正式阻止有声音的自动播放视频
- Firefox 加入新选项,可屏蔽带声音的视频自动播放
- USF MSDS501 计算数据科学中文讲义 1.3 播放声音
- Firefox 66 将自带自动屏蔽声音功能
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Web Development Recipes
Brian P. Hogan、Chris Warren、Mike Weber、Chris Johnson、Aaron Godin / Pragmatic Bookshelf / 2012-1-22 / USD 35.00
You'll see a full spectrum of cutting-edge web development techniques, from UI and eye candy recipes to solutions for data analysis, testing, and web hosting. Make buttons and content stand out with s......一起来看看 《Web Development Recipes》 这本书的介绍吧!