views:

59

answers:

3

I know it's generally frowned upon, but in this case I think it might actually be helpful. I want to play a little "ding" sound when a new item is posted on my site. I'll leave the option to turn it off of course.

Now should I do this with JavaScript? Would jQuery be helpful? Or should I use Flash? Looking for something that is widely compatible with different browsers. A link to a code sample or library would be helpful.

+2  A: 

Yep, use the jQuery Media plugin - it does exactly that :)

Marko
That'll convert my links into a media player/swf object... but I don't exactly want that. It needs to be invisible, and I need to trigger it with JS. It doesn't appear to have options for "restarting" the track or anything?
Mark
+1  A: 

Caveat...I'm not sure if this still works in modern browsers. But what I did in years past was simply write out an embed tag with an audio file as a source, ala:

document.write( '<embed src="some.wav"></embed>');

and the sound would be played when that was added to the page. It was actually used in a chat system and seemed to work ok. That being said, I have no doubt there's more modern, flash or ajaxy ways of doing this. But if you cant find another solution, this might work for ya.

GrandmasterB
Even if this works... it doesn't seem like a very elegant solution. Where's the preloading for instance?
Mark
I'm not talking books-on-cd here, just a beep that was a couple hundred bytes in size. Performance never even came up as an issue with it. And I did this almost 10 years ago, long before jquery, flash 10, and all that good stuff. Heck, you were lucky if the user had javascript!
GrandmasterB
@GrandmasterB: Actually, with a slight modification... this might be the way to go. Just embed the sound somewhere on the page exactly like this, hide it, turn off autostart and repeat, and then when you want to play it, just call `.Play()` on the object, rather than embedding dozens of these things. Simple, and seems to work. http://www.phon.ucl.ac.uk/home/mark/audio/play.htm
Mark
+1  A: 

You should probably look into the <audio> tag in HTML5 and then degrade it gracefully to flash/embed. Fore more advanced audio controls, FF4 beta has implemented an entire audio API, docs are here:

https://wiki.mozilla.org/Audio_Data_API

This is probably the direction audio on the web will take, but for wider support you should gracefully degrade it to a flash/embed solution.

Today, I think FF3.5+, Safari3+ and Chrome3+ supports the <audio> tag, but you should double-check codecs for Safari.

David