views:

10518

answers:

7

I am writing a dhtml application that creates an interactive simulation of a system. The data for the simulation is generated from another tool, and there is already a very large amount of legacy data.

Some steps in the simulation require that we play "voice-over" clips of audio. I've been unable to find an easy way to accomplish this across multiple browsers.

Soundmanager2 comes pretty close to what I need, but it will only play mp3 files, and the legacy data may contain some .wav files as well.

Does anyone have any other libraries that might help?

+5  A: 

I believe that the simplest and most convenient way would be to play the sound using a small Flash clip. I appreciate it's not a JavaScript solution but it IS the easiest way to achieve your goal

Some extra links from the previous similar question:

Ilya Kochetov
But I cannot find any flash solutions that will play WAV files. It appears flash only supports MP3.I'll look at scriptaculous's plugin, but I hate to include a whole extra library for this one bit of functionality.
Jake Stevenson
+6  A: 

If you're using Prototype, the Scriptaculous library has a sound API. jQuery appears to have a plugin, too.

ceejayoz
+11  A: 

You will have to include a plug-in like Real Audio or QuickTime to handle the .wav file, but this should work...

//======================================================================
var soundEmbed = null;
//======================================================================
function soundPlay(which)
    {
    if (!soundEmbed)
     {
     soundEmbed = document.createElement("embed");
     soundEmbed.setAttribute("src", "/snd/"+which+".wav");
     soundEmbed.setAttribute("hidden", true);
     soundEmbed.setAttribute("autostart", true);
     }
    else
     {
     document.body.removeChild(soundEmbed);
     soundEmbed.removed = true;
     soundEmbed = null;
     soundEmbed = document.createElement("embed");
     soundEmbed.setAttribute("src", "/snd/"+which+".wav");
     soundEmbed.setAttribute("hidden", true);
     soundEmbed.setAttribute("autostart", true);
     }
    soundEmbed.removed = false;
    document.body.appendChild(soundEmbed);
    }
//======================================================================
dacracot
I never understand down votes. This works. I have it deployed in my application. Why does it deserve a down vote?
dacracot
As the person who asked the question, I wonder too. It looks like a pretty good/simple solution to me. Would someone comment on what's wrong?
Jake Stevenson
I don't know why you were down voted, maybe for the use of the words "real audio" or "quicktime"..?? Maybe the person who downvoted you had no clue about how it was done? As for me, +1, your code is just about the same as the one used in the jQuery plugin stated in the most accepted answer...
m_oLogin
Sometimes I think it is because I wrote the code rather than called current popular API, which as you said, does the exact same thing.
dacracot
great solution to my problem as well. +1.
thephpdeveloper
however it doesn't seem to play the sound file in FF3.0. works in Chrome 3, IE7.
thephpdeveloper
+3  A: 

If you are using Mootools, there is the MooSound plugin.

cheeaun
A: 

Dacracot's solution works great, but I am afraid that id doesn't include the creation of tags, so it is not cross browser compatible.

Good Job Dacracot!!! :)

Caleb
A: 

dacracots code is clean basic dom, but perhaps written without a second thought? Of course you check the existance of an earlier embed first, and save the duplicate embed creation lines.

var soundEmbed = null; //=====================================================================

function soundPlay(which)
{
    if (soundEmbed)
       document.body.removeChild(soundEmbed);
    soundEmbed = document.createElement("embed");
    soundEmbed.setAttribute("src", "/snd/"+which+".wav");
    soundEmbed.setAttribute("hidden", true);
    soundEmbed.setAttribute("autostart", true);
    document.body.appendChild(soundEmbed);
}

Came across the thoughts here while scanning for a solution for somewhat similar situation. Unfortunately my browser Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.15) Gecko/2009102814 Ubuntu/8.04 (hardy) Firefox/3.0.15 dies when trying this.

After installing latest updates, firefox still crashes, opera keeps alive.

joshoreefe
A: 

You can use the HTML5 <audio> tag.

TTT