问题
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
- How can I play a short audio file very frequently?
- 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