[thelist] Can't get embedded media to play with JavaScript

Rick den Haan rick.denhaan at gmail.com
Thu Jul 19 05:20:09 CDT 2007


List,

I've been messing with this for almost two hours, and I still haven't gotten
it to work.  Here's the scenario:

On an AJAX-heavy application, sometimes sound effects are required. To do
this, on page load, a couple of WAV files are embedded into the HTML
document from an external JS file using DOM. I'm using EMBED here because
this should be cross-platform, and I don't want to force a particular
player. Who knows what plugin the visitor is using to play back a WAV file,
it could be WMP, QuickTime, RealPlayer, and who knows what else. So I'll let
the browser figure it out for itself.

// start
var oDiv = document.createElement('DIV');
oDiv.style.position = 'absolute';
oDiv.style.top = '-10000px';
oDiv.style.left = '-10000px';

var oEmbeds = new Array();

for (var i=0; i < aSoundList.length; i++) {
    oEmbeds[i] = document.createElement('EMBED');
    oEmbeds[i].id = 'sfx' + i;
    oEmbeds[i].src = aSoundList[i];
    oEmbeds[i].type = "audio/wav";
    oEmbeds[i].setAttribute('autostart', 'false');
    oEmbeds[i].setAttribute('loop', 'false');
    oEmbeds[i].setAttribute('hidden', 'true');

    oDiv.appendChild(oEmbeds[i]);
}

document.getElementsByTagName('BODY')[0].appendChild(oDiv);
// end

This works. When I view the generated source in Firefox using the Web
Developer Toolbar, the DIV is added, and all EMBEDs are in there, with the
attributes set correctly.

When a sound needs to be played, I want to start the appropriate EMBED. To
manage this somewhat, I have a variable iLastFX that contains the timestamp
of the last effect. If the user doesn't respond, I don't want the effect to
keep playing over and over. Right now I'm focusing on getting just a single
sound effect to work, I'll change that later.

Here's the function I have to play the sound:

// start
function playSound(iSound) {
    var iNow = (new Date()).getTime();
    if (typeof iLastFX != "undefined" && iNow > (iLastFX + 120000)) {
        // play again if two minutes have passed, timestamp is in
milliseconds
        iLastFX = iNow;
        var oEmbed = document.getElementById('sfx' + iSound);

        try {
            oEmbed.play();
        }
        catch (e) {
            try {
                oEmbed.doPlay();
            }
            catch (e) {
                try {
                    oEmbed.run();
                }
                catch (e) { }
            }
        }
    }
}
// end

doPlay should work if RealPlayer is used, run should work for Windows Media
Player, and play() should work in all other situations. For some reason,
nothing happens in FF or IE7 on Vista.

I tried this little sport:

// start
var oEmbed = document.getElementById('sfx' + iSound);
alert(oEmbed); // does it even exist?
for (var i in oEmbed) { alert (i + ": " + oEmbed[i]); }
// end

which gives me all properties the EMBED has. I can't find anything in there
that allows me to control playback.

The embeds are correct. If I set autostart to true, the sounds play, so the
paths are correct.

Can anyone shed some light on this for me? I'm sorry I can't post a link,
because this is an intranet application.

Thanks in advance,
Rick.




More information about the thelist mailing list