我需要使用AJAX为HTML页面加载一些WAV文件.我使用AJAX来获取WAV文件的详细信息,然后使用embed标签,我可以确认文件已成功加载,因为当我将自动启动设置为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));
}
Run Code Online (Sandbox Code Playgroud)
}
我使用以下代码播放文件(基于用户想要播放的声音文件)
function soundPlay(which) {
var sounder = document.getElementById(which);
sounder.Play();
}
Run Code Online (Sandbox Code Playgroud)
这里有些问题,因为我测试的浏览器都没有使用上面的代码播放文件.没有错误,代码只返回.
我会离开它(也就是说 - 我会说服客户将所有WAV转换为MP3并使用MooTools).但我意识到我可以播放没有动态嵌入的声音文件.
因此,相同的soundPlay函数适用于以下列方式嵌入的文件:
<embed src="/media/sounds/hug_sw1.wav" id="sound2" width="0" heigh="0" autostart="false" enablejavascript="true"/>
Run Code Online (Sandbox Code Playgroud)
HTML中的任何地方.
它在所有浏览器中都能很好地运行.
有人知道这个吗?这在所有浏览器中都是某种未记录的安全限制吗?(请记住,文件会动态预加载,因为我可以通过将autostart属性设置为true来确认 - 它们都会播放).
任何帮助赞赏.
| 归档时间: |
|
| 查看次数: |
10737 次 |
| 最近记录: |