6

我需要使用 AJAX 为 HTML 页面后台加载一些 WAV 文件。我使用 AJAX 获取 WAV 文件的详细信息,然后使用 embed 标记,我可以确认文件已成功加载,因为当我将 autostart 设置为 true 时,文件会播放。但是,我需要仅在用户单击按钮(或触发事件)时播放文件。以下是我预加载这些文件的代码:

function preloadMedia() {
  for(var i = 0; i < testQuestions.length; i++)  {
  var soundEmbed = document.createElement("embed");
  soundEmbed.setAttribute("src", "/media/sounds/" + testQuestions[i].mediaFile);
  soundEmbed.setAttribute("hidden", true);
  soundEmbed.setAttribute("id", testQuestions[i].id);
  soundEmbed.setAttribute("autostart", false);
  soundEmbed.setAttribute("width", 0);
  soundEmbed.setAttribute("height", 0);
  soundEmbed.setAttribute("enablejavascript", true);
  document.body.appendChild((soundEmbed));
}

}

我使用以下代码播放文件(基于用户想要播放的声音文件)

function soundPlay(which) {
  var sounder = document.getElementById(which);
  sounder.Play();
}

这里出了点问题,因为我测试过的浏览器都没有使用上面的代码播放文件。没有错误,代码只是返回。

我会把它留在那里(也就是说 - 我会说服客户将所有 WAV 转换为 MP3 并使用 MooTools)。但我意识到我可以播放不是动态嵌入的声音文件。

因此,相同的 soundPlay 函数适用于以下列方式嵌入的文件:

<embed src="/media/sounds/hug_sw1.wav" id="sound2" width="0" heigh="0" autostart="false" enablejavascript="true"/>

HTML 中的任何位置。

它在所有浏览器中都运行良好。

有人对此有任何线索吗?这是所有浏览器中某种未记录的安全限制吗?(请记住,文件确实是动态预加载的,我可以通过将 autostart 属性设置为 true 来确认 - 它们都在播放)。

任何帮助表示赞赏。

4

3 回答 3

1

嗯..也许,您需要在调用 preloadMedia() 之后等待嵌入对象加载其“src”?

您确定调用 soundPlay() 时加载了媒体文件吗?

于 2009-05-26T06:13:04.727 回答
0

我知道你的问题现在有点老了,但如果你仍然想知道......

soundEmbed.setAttribute("id", testQuestions[i].id);

您使用相同的id两次,但getElementById只返回一个元素,或者false如果它没有找到完全匹配的元素。

你可以尝试这样的事情:

soundEmbed.setAttribute("id", "soundEmbed_"+testQuestions[i].id);

始终牢记 aid必须是唯一的

于 2010-03-03T22:45:22.547 回答
0

只是为了提高兼容性的提示:我在这里读到,对于 MacOS 上的 Firefox,宽度和高度需要 > 0。

于 2012-05-16T10:13:12.483 回答