[thelist] random image+sound " popup"

Joshua Olson joshua at waetech.com
Tue May 17 13:35:59 CDT 2005


> -----Original Message-----
> From: Jeroen Wijers
> Sent: Tuesday, May 17, 2005 2:11 PM
> 
> Hi,
> 
> I am looking for a script/example that does something like this:
> randomly in time show and image somewhere ( layer technique 
> is fine even not all browser like it ), and plays a sound....

Jeroen,

You'll find out soon enough that the whole "Plays a sound" part of your
question is not as trivial as it seems.  Implementing a cross platform sound
solution is not straightforward.  

That being said, here's one that works in a good number of browsers (IE,
Moz, Opera).  Code is not object-oriented, on purpose, and only truly
supports playing one sound at a time:

var sounds = new Object();
var playing_sound;

function registerSound(name, filename)
{
  sounds[name] = filename;
}

function initSounds()
{
  document.write('<span id="soundspan">');
  if (navigator.userAgent.indexOf('Opera') == -1)
    for (i in sounds)
      document.write('<embed name="snd_' + i + '" id="snd_' + i + '" src="'
+ sounds[i] + '" loop="false" autostart="false" hidden="true"
enablejavascript="true" />');
    
  document.write('</span>');
}

function playSound(name) {
  playing_sound = document.getElementById('snd_' + name);
  // the embedded object won't exist if we are in Opera.
  if (playing_sound)
  {
    try 
    {
    playing_sound.Play();
    }
    catch (e)
    {
      // Just catch the error if it happens, please.
    }
  }
  else
  {
    // play sound in Opera
    playOperaSound(name);
  }
}

function stopSound() {
  if (playing_sound)
    playing_sound.Stop();
}

function playOperaSound(name) {
  document.all.soundspan.innerHTML = '<embed src="' + sounds[name] + '"
hidden="true" autostart="true" loop="false" />';
  // sound has started automatically, so clear the div.
  document.all.soundspan.innerHTML = '';
}


Usage:

<script ...>
  registerSound('ding', '/media/ding.wav');
  ...
  playSound('ding');
</script>

<><><><><><><><><><>
Joshua L. Olson
WAE Tech Inc.
http://www.waetech.com/
Phone: 706.210.0168 

Monitor bandwidth usage on IIS6 in real-time:
http://www.waetech.com/services/iisbm/




More information about the thelist mailing list