简体   繁体   English

播放mp3时听到回声

[英]Hearing an echo while playing mp3

I'm currently having problems while playing mp3 files on a website. 我目前在网站上播放mp3文件时遇到问题。

I'm using the following code to play an mp3 sound: 我正在使用以下代码播放mp3声音:

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. 声音非常短,从1到6秒不等。 According to some users the echo is sometimes so bad they can't understand what is being said (the mp3 files are spoken sentences). 一些用户认为,回声有时非常糟糕,以至于他们听不清说话的内容(mp3文件是口头表达)。 The echo usually stops after 2-3 seconds. 回声通常在2-3秒后停止。

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? 有谁知道这是怎么发生的?

If you're calling playSound in a click handler, could your users be double-clicking? 如果您在click处理程序中调用playSound ,您的用户是否可以双击? On most browsers, double-click causes two click events to occur (in addition to a dblclick event; example ). 在大多数浏览器中,双击会导致两个click事件发生(除了dblclick事件; 例如 )。 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. 您可能希望建立一些滞后作用-例如,在第一次单击之后,在500毫秒内忽略第二次(第三次,第四次)声音单击之类的事情。

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>");
      }
    }
}

您应该定义case isMozilla :由于许多浏览器都在使用UserAgent:Mozilla等...不仅是firefox浏览器本身,因此,如果您使用的是chrome或其他类型的OS浏览器,它们可能会从脚本中加载几种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM