Play a (short) sound extremely frequently?

南笙酒味 提交于 2019-12-11 13:11:19

问题


I want to create a metronome using setInterval. I want to be able to reach high bpms like 300 bpm. Even if the file is short enough to be played as many times as it's needed, it will hiccup very easily. Furthermore, many browsers have issues with short audio files - Safari for MP3 and Firefox for WAV. I tried using multiple files as you see below, to no avail.

    metronome = setInterval(function () {
        if (!tick.played) {
            tick.play();
        } else {
            tick2.play();
        }
    }, 200); // = 300 bmp
  1. How can I play a short audio file very frequently?
  2. What's the best file extension to do so?

回答1:


Use Web Audio API for accurate timing, JavaScript's setTimeout can have up to a 10ms offset. There's a metronome demo using audioContext.currentTime at it's core. See this article for details on audio scheduling. See this Plunker for Web Audio Metronome demo.



来源:https://stackoverflow.com/questions/35903805/play-a-short-sound-extremely-frequently

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!