views:

58

answers:

2

I'm currently having problems while playing mp3 files on a website.

I'm using the following code to play an mp3 sound:

function playSound(url){

  var userAgent    = navigator.userAgent.toLowerCase();
  var appVersion   = navigator.appVersion.toLowerCase();
  var appName      = navigator.appName.toLowerCase();
  var isOpera      = (userAgent.indexOf('opera') != -1);
  var isIE         = (appName.indexOf('internet explorer') != -1) && !isOpera;

  switch(true)
  {
    case isIE      :
      $("#soundSpan").html(" <bgsound src='"+url+"' />");
      break;
    default        :
      $("#soundSpan").html(" <embed src='"+url+"' type='audio/mpeg' autostart=true repeat=false loop=false hidden=true></embed>");
  }

}

This works fine for me and most of the users, but some users are complaining about hearing an echo. Meaning they are hearing the same sound multiple times(more than twice). The sounds are very short varying from 1 to 6 seconds. According to some users the echo is sometimes so bad they can't understand what is being said (the mp3 files are spoken sentences). The echo usually stops after 2-3 seconds.

I'm sure I'm playing the sound only once and the echo has appeared in different browsers.

Does anyone have an idea how this can happen?

A: 

If you're calling playSound in a click handler, could your users be double-clicking? On most browsers, double-click causes two click events to occur (in addition to a dblclick event; example). You might want to build in some hysteresis -- for instance, ignore a second (third, fourth) click on the sound for 500ms after the first click, that sort of thing.

Edit: Example:

var soundsPlayed = {};
function playSound(url){

  var userAgent    = navigator.userAgent.toLowerCase();
  var appVersion   = navigator.appVersion.toLowerCase();
  var appName      = navigator.appName.toLowerCase();
  var isOpera      = (userAgent.indexOf('opera') != -1);
  var isIE         = (appName.indexOf('internet explorer') != -1) && !isOpera;
  var soundPlayed  = soundsPlayed['p:' + url];
  var now          = new Date().getTime();

  // If we haven't played this sound, or we haven't played it in the
  // last half-second, go ahead and play it.
  if (!soundPlayed || (now - soundPlayed) > 500) {

      // Remember when we played it
      soundsPlayed['p:' + url] = now;

      // Play it
      switch(true) // true??
      {
        case isIE      :
          $("#soundSpan").html(" <bgsound src='"+url+"' />");
          break;
        default        :
          $("#soundSpan").html(" <embed src='"+url+"' type='audio/mpeg' autostart=true repeat=false loop=false hidden=true></embed>");
      }
    }
}
T.J. Crowder
Most of the times the sounds are played using a timer. http://jquery.offput.ca/every/ Sometimes a click handler is used, but the echo appears in both situations
@xta: Huh. Still may be worth adding a bit handling for it and seeing whether that makes a difference; it's dead easy, I've added an example above.
T.J. Crowder
I'll try and wait for my users to test, since I can't reproduce it myself. But this doesn't explain why only the first couple seconds of the sounds are echoing.
@xta: Ah, I didn't realize that (I thought the sounds were just short). Yeah, it doesn't. Good luck with it!
T.J. Crowder
A: 

You should define case isMozilla : since many browsers are using UserAgent: Mozilla, etc... Not only the firefox browser it self, so if you are using chrome or other kind of OS browsers they might load several cases from your script.

BoDiE2003
This is more a comment than an answer. Also, it's not like the code isn't handling the `isMozilla` case; in fact, it's the default (JavaScript, like C and C++, falls through by default from one case block to the next). The only special branch is `isIE`.
T.J. Crowder
I accidently removed that line, when I cleaned it up to post here. But I guess the line 'case isMozilla:' is irrelevent too.