如何在扩展中播放音频? [英] How to play audio in an extension?
问题描述
AudioObj =新的音频;
将会返回Audio is not definied
<我也尝试了经典的修复:
var audio = require(audio);
但没有运气。我可以在 main.js
的扩展的其他部分添加音频播放,就像其中的一些内容脚本一样,但也许有一个更简单更优雅的解决方案。 / p>
page-worker
模块并在那里注入内容脚本。然后,您可以发送消息到该内容脚本,并让它随时为您播放音频。 另一种方法是使用
$ b
var {Cc,Ci} = require(chrome);
var sound = Cc [@ mozilla.org/sound;1\"].createInstance(Ci.nsisound);
var uri = Cc [@ mozilla.org/network/io-service;1]
.getService(Ci.nsIIOService)
.newURI(self.data.url(.. 。),null,null);
sound.play(uri);
注意 nsISound
是很可能会被弃用。这是一个旧的API,不如HTML5音频。
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.
new 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.org/sound;1"].createInstance(Ci.nsISound);
var uri = Cc["@mozilla.org/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.
这篇关于如何在扩展中播放音频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!