通过Javascript播放动态嵌入的声音对象 [英] Playing dynamically embedded sound object via Javascript
问题描述
我需要后台加载一些WAV文件,使用AJAX的HTML页面。我使用AJAX来得到的WAV文件的详细信息,然后使用嵌入标记,我可以证实,这些文件已加载成功,因为当我设置自动启动为true,该文件播放。然而,我所需要的文件,只有当用户点击一个按钮(或事件触发)来播放。以下是我的code至preLOAD这些文件:
I need to background load some WAV files for an HTML page using AJAX. I use AJAX to get the details of the WAV files, then use the embed tag, and I can confirm that the files have loaded successfully because when I set autostart to true, the files play. However, I need the files to play only when the user clicks on a button (or an event is fired). The following is my code to preload these files:
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));
}
}
我用下面的code播放文件(基于用户想要玩什么声音文件)
I use the following code to play the file (based on what sound file that user wants to play)
function soundPlay(which) {
var sounder = document.getElementById(which);
sounder.Play();
}
有些事情错在这里,因为没有一个浏览器我玩已经测试使用上述code中的文件。有没有错误,并且code刚刚返回。
Something is wrong here, as none of the browsers I have tested on play the files using the code above. There are no errors, and the code just returns.
我会留在这一点(那就是 - 我会说服客户对所有WAV的转换成MP3和使用MooTools的)。但我意识到,我可以播放声音文件,这是不是动态内嵌。
I would have left it at that (that is - I would have convinced the client to convert all WAV's to MP3 and use MooTools). But I realized that I could play the sound files, which were not dynamically embeded.
因此,相同的soundPlay功能将工作包埋以下列方式的文件:
Thus, the same soundPlay function would work for a file embeded in the following manner:
<embed src="/media/sounds/hug_sw1.wav" id="sound2" width="0" heigh="0" autostart="false" enablejavascript="true"/>
在HTML内的任何地方。
anywhere within the HTML.
和它在所有的浏览器中打得很好。
And it plays well in all the browsers.
任何人有这样的线索?这是在所有的浏览器某种无证安全限制? (请记住,这些文件做动态获取preloaded,因为我可以通过自动启动属性设置为true确认 - 他们都发挥)。
Anyone have a clue on this? Is this some sort of undocumented security restriction in all the browsers? (Please remember that the files do get preloaded dynamically, as I can confirm by setting the autostart property to true - They all play).
任何帮助AP preciated。
Any help appreciated.
推荐答案
嗯..也许,你需要的等的为嵌入对象调用preloadMedia后装入其SRC( )?
Hmm.. perhaps, you need to wait for the embed object to load its "src" after calling preloadMedia() ?
您肯定当你调用soundPlay()?媒体文件加载
Are you sure that the media file is loaded when you call soundPlay() ?
这篇关于通过Javascript播放动态嵌入的声音对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!