I just came across this article from Thomas Fuchs , which covers several ways to play sound through Javascript, without relying on a sudden drop.
Here are the main points of the article .
Mobile Browsers
While there is no solution, if the sound will not play as a direct result of the userβs action (click)
Browsers with support for the HTML5 <audio> element.
It is required to provide audio in three different formats.
if("Audio" in window){ var a = new Audio(); if(!!(a.canPlayType && a.canPlayType('audio/ogg; codecs="vorbis"').replace(/no/, ''))) a.src = "/sounds/ping.ogg"; else if(!!(a.canPlayType && a.canPlayType('audio/mpeg;').replace(/no/, ''))) a.src = "/sounds/ping.mp3"; else if(!!(a.canPlayType && a.canPlayType('audio/mp4; codecs="mp4a.40.2"').replace(/no/, ''))) a.src = "/sounds/ping.m4a"; else a.src = "/sounds/ping.mp3"; a.autoplay = true; return; }
If you are dealing with IE <9
<bgsound src="/sounds/ping.mp3" loop="1" autostart="autostart">
Otherwise, support for testing QuickTime, RealPlayer and Windows Media
The following code is written with Prototype.js
// this code uses Prototype.js if(navigator.plugins && $A(navigator.plugins).detect(function(p){ return p.name.indexOf('QuickTime') != -1 })) Sound.template = new Template('<object id="sound_#{track}_#{id}" width="0" height="0" type="audio/mpeg" data="#{url}"/>'); else if(navigator.plugins && $A(navigator.plugins).detect(function(p){ return p.name.indexOf('Windows Media') != -1 })) Sound.template = new Template('<object id="sound_#{track}_#{id}" type="application/x-mplayer2" data="#{url}"></object>'); else if(navigator.plugins && $A(navigator.plugins).detect(function(p){ return p.name.indexOf('RealPlayer') != -1 })) Sound.template = new Template('<embed type="audio/x-pn-realaudio-plugin" style="height:0" id="sound_#{track}_#{id}" src="#{url}" loop="false" autostart="true" hidden="true"/>');
jessegavin
source share