Dynamically add an 'Embed' type element to a document from within XUL
This might sound vague, but I've suffered the last 6 hours attempting to solve this "simple" feat:
- I have code running within a Firefox Addon
- User triggers an action that requires some audio to be played back, but the catch here is - this audio is dynamic (depends in input from user), and is being fetched from a server somewhere.
- I choose to dynamically insert an embedelement into a newtabbrowser's document with itssrcpointing towards the server resource, but am TOTALLY FAILLING to make this work! I failed to embed the element!
Funny thing is that it works well from no开发者_JS百科rmal javascript function scope (code within a script tag in an html document), but all fails when am using XUL.
Below is come code snippet
function loadURL(url)  
{
var razorTabBrowser = gBrowser.getBrowserForTab(gBrowser.addTab("chrome://razorextension/content/read.html"));  
razorTabBrowser.addEventListener("load", function () { 
    var soundEmbed = razorTabBrowser.contentDocument.createElement("embed");
     soundEmbed.setAttribute("src", url);
     soundEmbed.setAttribute("hidden", true);
     soundEmbed.setAttribute("autostart", true);
     soundEmbed.removed = false;
     razorTabBrowser.contentDocument.body.appendChild(soundEmbed)
    }, true);
}
As far as I know, nsISound.play works on remote URLs, as long as it's WAV format.
EDIT:
I can't remember which version of Firefox supports <html:audio> but I think you can use it without even adding it to a document.
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论