Implementing <bgsound>

I never thought I’d say this, but I want to write an extension to implement <bgsound>. The idea is that, once the extension is installed, it works just like in IE (as close as possible, at any rate) – including being correctly manipulated by script. What’s the best way to do it? Having it only work in Firefox 1.5 and above would be OK.

My initial idea is to use the new 1.5 interface for adding content CSS stylesheets to bind an XBL behaviour to the <bgsound> tag, which then implements the function using the nsISound interface to get simple playback capability. Is this reasonable? Am I going to run into security problems?

You are all welcome to speculate as to exactly why I would want to do this; I am neither going to confirm or deny any suggestions. :-)

26 thoughts on “Implementing <bgsound>

  1. One would assume you’re doing it for the practical reason that there’s still no rational way to implement cross-browser audio support and that bgsound isn’t inherently any more evil than, say, the fact that text-decoration: blink doesn’t have a -moz in front of it. Of course you could just like pain.

    – Chris

  2. text-decoration: blink doesn’t need a vendor-specific part as it’s actually specified in CSS and not vendor-specific at all. (<blink> should properly be <moz:blink> or some such, though.)

    Personally I think that, more than liking pain, Gerv is just jealous of Doron and all the attention he’s gotten over the years for marquee. I predict however that as long as bgsound only remains an extension, this cunning ploy to take the lead in the rankings of most reviled mozilla dev will fail. ^_^

  3. I believe there is a bug in remote XUL which prevents it from accessing XPCOM-defined properties cleanly. I’m almost certainly cc’d on it, but I’m having trouble finding it at the moment. Beware.

  4. The first thing that I thought of was “XSL! <object> tags!”. Then I remembered these are pages that use <bgsound> we’re talking about.

    Maybe a javascript find/replace with innerHTML and regex might work?

  5. An XBL binding could add an as anonymous content, inheriting the right attributes, maybe.

  6. an <object> that is (why does this software not escape < and > automatically?)

  7. You will run into a security problem. XBL bindings run in the security context of the content they’re attached to, which means you won’t have access to nsISound. Our workaround for this in Flashblock was to generate an event, and have code in the browser overlay handle the event and do the interfacing with XPCOM. I’m sure you could implement this similarly, by having your browser overlay handle the event and play the associated sound.

    You can see the details of our implementation here:
    and here:

    Look for “flashblockCheckLoad”.

  8. > there’s still no rational way to implement cross-browser audio support

    The techniques listed in work, but most browsers bring up an annoying helper app window. It would be nice if browsers knew how to play simple sound formats themselves, just like they can display simple image formats like JPEG, GIF, and PNG.

  9. Not having read much about this tag, does it play sounds? If so, you might find my xulmusic implementation helpful, I implemented an audio interface using quicktime, directx, and gstreamer for mac, win32, and linux…

  10. I remember there once being an extension that did just this.
    I assume you’ve already googled for it?
    (I could be halucinating again)

  11. Converting it into an embed/object using xbl would be best, allowing the plugin system to choose a media player for you. That would also enable the plugin finder in case no player exists, which might be a bonus :)

  12. I tried to write a quick XBL binding for this a long time ago. At the time, there was a weird issue where the bgsound element was automaticly moved out of the body into the head. (Or something like that, it’s been a while.) It made it hard to solve the problem just using XBL, but that might be fixed now. (Or it might have been some other error on my part to begin with. ^_^)

  13. also, nsISound’s implementation’s vary widely in quality. On some platforms it’s synchronous, on others it’s not and so on.

  14. Don’t look to implement this as an extension – get the support directly into the main Mozilla/Firefox codebases, so we can all use it without needing to go through the considerable hassle of installing a separate extension.

  15. NOOOOOO!!!

    Don’t get it in the main codebase. Please. In fact, make it an extension which is difficult to find and tougher to install.

    Xanga is bad enough as it is.

  16. This shouldn’t really need to be done at all – bgsound is a proprietary relic from the NS/IE4 days and encouraging its use when there’s a replacement (object) is just wrong. Adding support for it to Gecko directly is even worse, considering people are trying to reduce codebase bloat.

  17. doron: would such a media player be UI-less? Would it be possible to fall back on using nsISound if there were no suitable plugin available? I’d rather do that than have people messing with the plugin finder.

    Regarding formats and nsISound implementations: in the particular case I need it for, I only need it to play one particular format, which I believe nsISound can cope with. While it would be nice if it played others, it’s not important. The variation across platforms might be of concern.

    The key features are scriptable play and stop – actually getting a WAV referenced by a URL played out of the user’s speakers. Everything else is gravy.

  18. I’m against implementing a proprietary <bgsound> for HTML, but why not for XUL? Something like an aural XUL widget ( might be useful for extension developers that would be relieved from using nsISound themselves.

  19. Gerv, nsISound is pretty much broken…. For example, on some platforms it blocks until the sound is done playing (that means no UI interaction, no repainting, nothing). It’s ok for very short system-sound like sounds, but that’s about it.

  20. The worst is Opera – it supports BGSOUND and EMBED… therefore when visiting a page with both on, supposedly for cross-browser sound, they both play, and not quite at the same time either!

  21. Yes, >bgsound< is the only tag that I know that decently implemnts music (and speach) allowing to make multimedia with DHTML. Could you implement it so it allows mp3’s to play as well.

    And while at it: onmouseover and onclick events.
    In this way Mozilla would be THE DHTML browser. I am so tired to tell people that my site only works on MSIE, because I simply cannot find out of making cross browser applications.