playing short sound files without controls on the page

Discussion in 'Javascript' started by laredotornado, Feb 2, 2007.

  1. Hi,

    Is there a cross-browser way to play short (< 25K) sound files without
    spawning new windows or embedding any visual controls on the page? I
    would like to click a button and hear my short sound.

    Thanks for your help, - Dave

    ps - Searching Google turned up stuff for Netsacpe and IE 3, but I was
    hoping more for IE 7 and Firefox.
    laredotornado, Feb 2, 2007
    1. Advertisements

  2. laredotornado

    getburl Guest

    Hey Dave,

    The easiest way is to attach an 'embed' element and then change the
    src attribute. This willl load the file at that time and play it when
    it is loaded.

    I also wrote a small sound class for a particular need I had. I posted
    it here

    getburl, Feb 2, 2007
    1. Advertisements

  3. Thanks for posting this. This is a very cleanly written class. I do
    have one question. is calling the "draw" function the equivalent of
    calling play? So, if someone clicked on a button labeled "Play",
    would the right JS be

    var s = new Sound();
    s.initialize('/path/to/my/sound', null, 'arbitraryId');


    Thanks again, - Dave
    laredotornado, Feb 2, 2007
  4. laredotornado

    getburl Guest

    Sort of. I called it draw because that is more literal. Render would
    be even more literal because the DOM element is being inserted into
    the DOM of the page. So, the element is being drawn or rendered on the
    page. Then there is load time until the sound plays. So, you could
    rename it to play if that is more intuitive. Very good point. I may
    have to change that.
    getburl, Feb 2, 2007
  5. laredotornado

    getburl Guest

    I did not read your code quite as well as I should have. The correct
    instantiation would be:

    var s = new Sound('/path/to/my/sound', null, 'arbitraryId'); //
    initialization is implicit when the object is instantiated
    getburl, Feb 2, 2007
  6. Thanks. Just one more thing. When I load this script in the latest
    version of Firefox on the PC, I'm getting a "Class is not defined"
    error, supposedly referring to the line

    var Sound = Class.create();

    Was there something else I was supposed to include in addition to the
    lines posted on your blog?

    - Dave
    laredotornado, Feb 2, 2007
  7. laredotornado

    getburl Guest

    Hey Dave,

    Yes, the class is based on the Prototype Framework found at http://
    The Sound class I wrote could be ported to a standalone class. I wrote
    it based on Prototype out of my own specific need.

    getburl, Feb 3, 2007
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.