admin管理员组文章数量:1336632
AudioObj = new Audio;
will return "Audio is not definied"
I also tried the classic fix:
var audio = require("audio");
but with no luck. I could add audio playback in some other part of the extension than in main.js
, like some content script where it works, but maybe there is a simpler and elegant solution.
AudioObj = new Audio;
will return "Audio is not definied"
I also tried the classic fix:
var audio = require("audio");
but with no luck. I could add audio playback in some other part of the extension than in main.js
, like some content script where it works, but maybe there is a simpler and elegant solution.
- Could you use jPlayer instead? – Larry Battle Commented May 19, 2012 at 3:46
- What happens when you console.log( new Audio ); or console.dir( Audio ) – Larry Battle Commented May 19, 2012 at 4:04
- console logs just returns that audio is not defined. I had a look at jPlayer and it seems more of a way to create a player in html and controlling and styling it... also I am not really sure how would I use it in main.js, short of pasting it all in.. – DoTheEvo Commented May 19, 2012 at 10:54
3 Answers
Reset to default 6new Audio
creates a new <audio>
HTML element - this only works in a context that is bound to a document. SDK modules execute in a context however that has no document, consequently no DOM methods will work including this one. A work-around would be loading about:blank
via page-worker
module and injecting a content script there. You could then send messages to that content script and let it play audio for you whenever you need it.
The alternative would be using nsISound.play()
, something along these lines:
var {Cc, Ci} = require("chrome");
var sound = Cc["@mozilla/sound;1"].createInstance(Ci.nsISound);
var uri = Cc["@mozilla/network/io-service;1"]
.getService(Ci.nsIIOService)
.newURI(self.data.url(...), null, null);
sound.play(uri);
Note that nsISound
is likely to be deprecated soon. It is an old API that is inferior to HTML5 audio.
var window = require("sdk/window/utils").getMostRecentBrowserWindow();
AudioObj = new window.Audio;
main.js does not have access to the DOM apis that typically exist in web content. If you want to use DOM apis, you need to use content scripts. For more on how the SDK module system works, please see the docs:
https://addons.mozilla/en-US/developers/docs/sdk/1.7/dev-guide/guides/monjs.html
本文标签: javascriptHow to play audio in an extensionStack Overflow
版权声明:本文标题:javascript - How to play audio in an extension? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742414445a2470436.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论