javascript - Audio tag playing sound only once, onclick - Safari
问题描述
I was making this little practice project for myself, a drum machine, where you press a button - you get a certain sound. Everything worked fine until I opened it in Safari. It plays the sound once, then it kind of does it after, but the audio sounds glitched and so quiet that I initially thought there was no sound at all. I have attached the link to codepen. Granted, there are no sounds in codepen version as I don't really know where to easily upload them. If someone knows a good way to do that, I will gladly add it as well.
<div data-key="KeyA" class="button flex">
<h1>A</h1>
<h2>Kick</h2>
<audio data-key="KeyA" src="./sounds/kick.wav"></audio>
</div>
<div data-key="KeyS" class="button flex">
<h1>S</h1>
<h2>Snare</h2>
<audio data-key="KeyS" src="./sounds/snare.wav"></audio>
</div>
...
const buttons = document.querySelectorAll('.button');
buttons.forEach(button => {
button.addEventListener('click', e => {
const sound = document.querySelector(`audio[data-key="${button.dataset.key}"]`);
sound.currentTime = 0;
sound.play();
window.addEventListener('keydown', e => {
const sound = document.querySelector(`audio[data-key="${e.code}"]`);
sound.currentTime = 0;
sound.play();
boxPress(e);
});
解决方案
推荐阅读
- swift - 将magicCookie从音频文件复制到音频队列失败
- sqlite - SQLite.SQLiteException:数据库磁盘映像在 xamarin.forms 应用程序中格式错误
- python - 使用 Stackexchange API 检索答案和评论的文本正文
- java - 尝试从 Excel 获取值并输入到 Class 时如何修复“java.lang.ArrayIndexOutOfBoundsException”
- python - 使用hackerrank中的请求登录Google Oauth2
- google-chrome - Leaflet.Control.Layers.Tree 在 Chrome 中无法正确显示菜单
- angular - FormGroup 表单错误为空 Angular Reactive Forms
- php - router-view 不渲染路由和组件
- c# - 使用 ActivityPrompt 从 webchat BOT Framework 4 获取自定义事件
- sql - SQL 从多个表中选择并计算第三个