【问题标题】:Hearing an echo while playing mp3播放 mp3 时听到回声
【发布时间】:2010-09-06 13:53:28
【问题描述】:

我目前在网站上播放 mp3 文件时遇到问题。

我正在使用以下代码播放 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>");
  }

}

这对我和大多数用户来说都很好,但有些用户抱怨听到回声。这意味着他们多次听到相同的声音(超过两次)。声音很短,从 1 到 6 秒不等。根据一些用户的说法,回声有时非常糟糕,以至于他们无法理解所说的内容(mp3 文件是口语句子)。回声通常在 2-3 秒后停止。

我确定我只播放了一次声音,并且回声出现在不同的浏览器中。

有人知道这是怎么发生的吗?

【问题讨论】:

  • 你在哪里打电话playSound()
  • playSound函数什么时候调用?
  • “isMozilla”在哪里以及如何定义?许多用户代理包含一个“Mozilla”字符串
  • @BatchyX:很好,但因为它也是默认值,所以这并不重要。 :-)
  • playSound 大多数时候是从计时器jquery.offput.ca/every 调用的。我删除了 isMozilla 行,因为它不需要。

标签: javascript html mp3


【解决方案1】:

您应该定义case isMozilla :,因为许多浏览器都在使用 UserAgent:Mozilla 等...不仅是 firefox 浏览器本身,因此如果您使用 chrome 或其他类型的操作系统浏览器,它们可能会从您的脚本中加载几个案例.

【讨论】:

  • 这更像是一个评论而不是一个答案。此外,代码并不是没有处理isMozilla 的情况;事实上,这是默认设置(JavaScript 与 C 和 C++ 一样,默认情况下会从一个 case 块跳到下一个 case 块)。唯一的特殊分支是isIE
  • 我不小心删除了那行,当我清理它在这里发布时。但我想“case isMozilla:”这句话也无关紧要。
【解决方案2】:

如果您在click 处理程序中调用playSound,您的用户会双击吗?在大多数浏览器上,双击会导致两个click 事件发生(除了dblclick 事件;example)。您可能希望建立一些滞后性——例如,在第一次点击后 500 毫秒内忽略第二次(第三次、第四次)点击声音,诸如此类。

编辑:示例:

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

【讨论】:

  • 大多数情况下,声音是使用计时器播放的。 jquery.offput.ca/every 有时会使用点击处理程序,但在两种情况下都会出现回显
  • @xta:嗯。仍然可能值得为它添加一些处理并查看是否有所作为;这很简单,我在上面添加了一个示例。
  • 我会尝试等待我的用户进行测试,因为我自己无法重现它。但这并不能解释为什么只有前几秒的声音在回响。
  • @xta:啊,我没有意识到(我以为声音很短)。是的,它没有。祝你好运!
猜你喜欢
  • 2011-06-07
  • 1970-01-01
  • 1970-01-01
  • 2011-01-07
  • 2016-05-29
  • 1970-01-01
  • 1970-01-01
  • 2013-03-26
  • 1970-01-01
相关资源
最近更新 更多