[Javascript] sounds

Steve Clay sclay at ufl.edu
Wed Jun 21 12:47:57 CDT 2006


Tuesday, June 20, 2006, 11:05:59 PM, tedd wrote:
> So, I would like to know if there is an "approved" way of doing this, is there?

I think the most reliable way to trigger a sound in Javascript would be to
dynamically create a Flash object (google SWFObject) using a SWF that
just plays the sound specified in the FlashVars. Of course this limits you
to the sound formats Flash supports.

--
BTW, Opera 9 is supposed to support the new WebApps Audio object:
http://whatwg.org/specs/web-apps/current-work/#audio

new Audio("test.wav").play(); // create object, play clip once.

You could fake this interface using the above Flash approach, then you'd
have easy cross-browser sound. Something like this (untested):

if (typeof Audio == 'undefined') {
  function Audio(uri, swfContainer) {
    this.uri = uri;
    window.Audio_guid = window.Audio_guid ? (window.Audio_guid + 1) : 0;
    this._guid = window.Audio_guid;
    if (!swfContainer) {
      swfContainer = document.getElementsByTagName('body')[0].appendChild(
        document.createElement('span')
      );
    }
    swfContainer.id = 'Audio_cont' + this._guid;
    this.swfContainer = swfContainer;
  }
  Audio.prototype.play = function() {
    var so = new SWFObject("Audio.swf", "Audio_object" + this._guid, "1", "1");
    so.addParam("wmode", "transparent");
    so.addParam("flashvars", "file=" + escape(this.uri));
    so.write("Audio_cont" + this._guid);
  };
}

Steve
-- 
http://mrclay.org/




More information about the Javascript mailing list