by wavefunction · 12y ago · 0 comments
I really should probably write this up on my blog... I will - and post it on HN but here's a quick overview. I'm doing this for a JavaScript library I'm working on for form-validation for the visually-impaired (coming soon!)
I dynamically create a pool of audio tags via
var audioTag = document.createElement("audio")
then
audioTag.setAttribute("autoplay","autoplay");
then
audioTag.src = strSound;
You don't even have to attach them to the DOM either from my experience so far (cross-browser!), just maintain the reference in memory.
Then I do some eventing magic for forms and validation stuff, loading the audio from strings of Base64 encoded mp3s or wavs. This way I can package the audio I use inside the JavaScript that hooks up all the form events. And make it easy for others to override my audio samples with their own just by over-writing the strings in memory via their own .js file include.
I'm working up a simplified example to post right now. My library has a lot of extraneous stuff related to cross-browser eventing and integrates with browser extensions, so I think it would be needlessly confusing to illustrate what we're discussing