Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cross-platform, cross-browser way to play sound from Javascript? [duplicate]

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?

like image 377
Jake Stevenson Avatar asked Oct 09 '08 12:10

Jake Stevenson


4 Answers

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);
    }
//======================================================================
like image 52
dacracot Avatar answered Oct 20 '22 19:10

dacracot


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

like image 23
ceejayoz Avatar answered Oct 20 '22 21:10

ceejayoz


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.

like image 8
joshoreefe Avatar answered Oct 20 '22 21:10

joshoreefe


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:

  • Scriptaculous, a Javascript library: http://github.com/madrobby/scriptaculous/wikis/sound
  • an opensource Flash project: Easy Musicplayer For Flash http://emff.sourceforge.net/
like image 7
Ilya Kochetov Avatar answered Oct 20 '22 21:10

Ilya Kochetov