Wednesday, January 31st, 2007
Sound with JavaScript but not Flash
<p>Reinier Zwitserloot wanted to see if he could add sound support without embedding a Flash bridge, and shared his research in his article on Sound in Web Browsers without Flash.Check out his test page for a Sound Check
An example API
- function sound2Play() {
- if ( !sound2Embed ) {
- sound2Embed = document.createElement("embed");
- sound2Embed.setAttribute("src", "machinegun.wav");
- sound2Embed.setAttribute("hidden", true);
- sound2Embed.setAttribute("autostart", true);
- } else sound2Stop();
- sound2Embed.removed = false;
- document.body.appendChild(sound2Embed);
- }
- function sound2Stop() {
- if ( sound2Embed && !sound2Embed.removed ) {
- document.body.removeChild(sound2Embed);
- sound2Embed.removed = true;
- }
- }








Very interesting, thanks!
Well sure, have fun loading huge .wav files!
Nice idea, but unfortunately it doesn’t work without QuickTime plugin – tested with Firefox 2.0 Win.
Good work! How relevant is the QT plugin, shame if that’s a requirement, but then a pretty decent chunk of users have it.
As well, using an embed tag in IE7 can cause a security dialog to popup. The bgsound element, as old and junky as it is, circumvents this problem (only works in IE though).
Sweet, web2.0 meets geocities!
firefox 1.0.6 (earlier firefox versions were not working well, hanging sometimes, so I’ve downgraded mine) tells me that I need a missing plugin but it can’t find any to install. running on suse 9
should newer browsers come with some basic multimedia support?
Websites with sound are so annoying. I hate it.
I’m always trying to listen to music but end up with there rubbish on.
This is good stuff and useful for when people just simply want a soundtrack – “I just want sound, man! :)” (I wrote SoundManager V1/V2, but can appreciate simplicity as well as non-reliance on Flash.) It can be kind of tricky trying to detect support for installed plugins, getting a particular player to handle mp3/wav etc. and so on, but this looks like a good approach. I think IE makes it really simple with the “bgsound” attribute, others need some more wrangling. The first three buttons prompted me to install a plugin in Firefox 2.0.0.1, as did the “mp3″ one. The Midi worked, however! You can script Windows Media Player and QuickTime to some degree via Javascript, but support across browsers can be a bit sketchy.
I’m interested in the Web App interface as supported by Opera, this sounds potentially awesome. Now if could just be adopted elsewhere.. :)
Anybody else wishing for a seek(index) method on the Audio interface?
This could be pretty useful for multimedia applications in the browser making programmatic mixing and editing of sound accesible to a wide audience.
Wow, am I glad none of the methods worked on Opera! You can keep your noise-infested pages to yourselves.
How do I add a sound on the index page of my website using body onload? I’ve tried a few times but I keep on getting the wav sound to play showing the Quicktime program instead of my front page!
Very interesting proof of concepts.
I did some testing and it dose not work on firefox 2 and opera mobile 8.65. May be some other people got the same results as mine.
very interresting
i was wondering does this work on all platforms? i really like the concept
@Darek: Opera supports the Audio interface, but unfortunately, the Wii version of Opera does not. Anybody who tells me how to invoke sound from JavaScript on the Wii is my hero!