Tuesday 15 November 2011
Latency Of HTML5 <audio> Sounds
One of the common complaints about APIs for Web gaming is that there's no standard API to just play sounds in response to game events with low latency.
However, the <audio> element API is perfectly adequate for this. Use <audio src="shot.wav" id="shot" preload>
and every time you want to play the sound, use document.getElementById("shot").cloneNode(true).play()
. The HTML5 spec says that the browser can use the same resource data for the clone as for the original element (no HTML request should ever be needed). The cloned node is not in the document and has no event handlers so most of the overhead of media elements can be optimized away. If there are latency issues, then they are simply browser implementation issues that we should fix. New API is not needed here, and it hardly ever makes sense to respond to browsers poorly implementing one API by asking them to implement another one.
I believe people who say these <audio> implementation issues exist. Unfortunately, in our limited testing the latency of cloning and playing an <audio> element is pretty good in Firefox and Chrome on Window. So from people encountering problems in this area, we really need actionable bugs with testcases to be filed against the offending browsers. Please!!!
Comments