我正在制作 Firefox 扩展程序,但无法播放位于附加组件data
目录中的声音。
我尝试的第一件事是以这种方式在内容脚本中播放它:
var soundFile = self.options.soundFile; (new Audio(soundFile)).play();
where
self.options.soundFile
是一个选项,它引用data
目录中的资源文件。但我符合安全限制:Security Error: Content at http://example.com may not load or link to resource://jid0-a02no8rrtu2pbize7g7sszzo0z8-at-jetpack/stackoverflow-reiew-helper/data/complete.oga.
然后我找到了一种在
main.js
脚本中播放声音的方法(这里:如何在扩展中播放音频?):var data = require('sdk/self').data; exports.main = function() { var {Cc, Ci} = require("chrome"); var sound = Cc["@mozilla.org/sound;1"].createInstance(Ci.nsISound); sound.play(data.url('complete.oga')); };
这个失败了,但有以下例外:
NS_ERROR_XPC_BAD_CONVERT_JS: Could not convert JavaScript argument arg 0 [nsISound.play] undefined 8 Traceback (most recent call last): File "resource://gre/modules/NetUtil.jsm", line 140, in null aCallback(pipe.inputStream, aStatusCode, aRequest); File "resource://jid0-a02no8rrtu2pbize7g7sszzo0z8-at-jetpack/addon-sdk/lib/sdk/net/url.js", line 49, in null resolve(data); File "resource://jid0-a02no8rrtu2pbize7g7sszzo0z8-at-jetpack/addon-sdk/lib/sdk/core/promise.js", line 143, in resolve while (pending.length) result.then.apply(result, pending.shift()) File "resource://jid0-a02no8rrtu2pbize7g7sszzo0z8-at-jetpack/addon-sdk/lib/sdk/core/promise.js", line 37, in then return { then: function then(resolve) { resolve(value) } } File "resource://jid0-a02no8rrtu2pbize7g7sszzo0z8-at-jetpack/addon-sdk/lib/sdk/core/promise.js", line 117, in resolved function resolved(value) { deferred.resolve(resolve(value)) } File "resource://jid0-a02no8rrtu2pbize7g7sszzo0z8-at-jetpack/addon-sdk/lib/sdk/core/promise.js", line 143, in resolve while (pending.length) result.then.apply(result, pending.shift()) File "resource://jid0-a02no8rrtu2pbize7g7sszzo0z8-at-jetpack/addon-sdk/lib/sdk/core/promise.js", line 37, in then return { then: function then(resolve) { resolve(value) } } File "resource://jid0-a02no8rrtu2pbize7g7sszzo0z8-at-jetpack/addon-sdk/lib/sdk/core/promise.js", line 117, in resolved function resolved(value) { deferred.resolve(resolve(value)) } File "resource://jid0-a02no8rrtu2pbize7g7sszzo0z8-at-jetpack/addon-sdk/lib/sdk/core/promise.js", line 143, in resolve while (pending.length) result.then.apply(result, pending.shift()) File "resource://jid0-a02no8rrtu2pbize7g7sszzo0z8-at-jetpack/addon-sdk/lib/sdk/core/promise.js", line 37, in then return { then: function then(resolve) { resolve(value) } } File "resource://jid0-a02no8rrtu2pbize7g7sszzo0z8-at-jetpack/addon-sdk/lib/sdk/core/promise.js", line 117, in resolved function resolved(value) { deferred.resolve(resolve(value)) } File "resource://jid0-a02no8rrtu2pbize7g7sszzo0z8-at-jetpack/addon-sdk/lib/sdk/core/promise.js", line 143, in resolve while (pending.length) result.then.apply(result, pending.shift()) File "resource://jid0-a02no8rrtu2pbize7g7sszzo0z8-at-jetpack/addon-sdk/lib/sdk/core/promise.js", line 37, in then return { then: function then(resolve) { resolve(value) } } File "resource://jid0-a02no8rrtu2pbize7g7sszzo0z8-at-jetpack/addon-sdk/lib/sdk/core/promise.js", line 117, in resolved function resolved(value) { deferred.resolve(resolve(value)) } File "resource://jid0-a02no8rrtu2pbize7g7sszzo0z8-at-jetpack/addon-sdk/lib/sdk/core/promise.js", line 143, in resolve while (pending.length) result.then.apply(result, pending.shift()) File "resource://jid0-a02no8rrtu2pbize7g7sszzo0z8-at-jetpack/addon-sdk/lib/sdk/core/promise.js", line 37, in then return { then: function then(resolve) { resolve(value) } } File "resource://jid0-a02no8rrtu2pbize7g7sszzo0z8-at-jetpack/addon-sdk/lib/sdk/core/promise.js", line 117, in resolved function resolved(value) { deferred.resolve(resolve(value)) } File "resource://jid0-a02no8rrtu2pbize7g7sszzo0z8-at-jetpack/addon-sdk/lib/sdk/core/promise.js", line 143, in resolve while (pending.length) result.then.apply(result, pending.shift()) File "resource://jid0-a02no8rrtu2pbize7g7sszzo0z8-at-jetpack/addon-sdk/lib/sdk/core/promise.js", line 123, in then else result.then(resolved, rejected) File "resource://jid0-a02no8rrtu2pbize7g7sszzo0z8-at-jetpack/addon-sdk/lib/sdk/core/promise.js", line 37, in then return { then: function then(resolve) { resolve(value) } } File "resource://jid0-a02no8rrtu2pbize7g7sszzo0z8-at-jetpack/addon-sdk/lib/sdk/core/promise.js", line 117, in resolved function resolved(value) { deferred.resolve(resolve(value)) } File "resource://jid0-a02no8rrtu2pbize7g7sszzo0z8-at-jetpack/addon-sdk/lib/sdk/core/promise.js", line 55, in effort try { return f(options) } File "resource://jid0-a02no8rrtu2pbize7g7sszzo0z8-at-jetpack/addon-sdk/lib/sdk/core/promise.js", line 117, in resolved function resolved(value) { deferred.resolve(resolve(value)) } File "resource://jid0-a02no8rrtu2pbize7g7sszzo0z8-at-jetpack/addon-sdk/lib/sdk/core/promise.js", line 143, in resolve while (pending.length) result.then.apply(result, pending.shift()) File "resource://jid0-a02no8rrtu2pbize7g7sszzo0z8-at-jetpack/addon-sdk/lib/sdk/core/promise.js", line 37, in then return { then: function then(resolve) { resolve(value) } } File "resource://jid0-a02no8rrtu2pbize7g7sszzo0z8-at-jetpack/addon-sdk/lib/sdk/core/promise.js", line 117, in resolved function resolved(value) { deferred.resolve(resolve(value)) } File "resource://jid0-a02no8rrtu2pbize7g7sszzo0z8-at-jetpack/addon-sdk/lib/sdk/core/promise.js", line 55, in effort try { return f(options) } File "resource://jid0-a02no8rrtu2pbize7g7sszzo0z8-at-jetpack/addon-sdk/lib/sdk/addon/runner.js", line 90, in onLocalizationReady run(options); File "resource://jid0-a02no8rrtu2pbize7g7sszzo0z8-at-jetpack/addon-sdk/lib/sdk/addon/runner.js", line 134, in run quit: exit File "resource://jid0-a02no8rrtu2pbize7g7sszzo0z8-at-jetpack/stackoverflow-reiew-helper/lib/main.js", line 8, in exports.main sound.play(data.url('complete.oga'));
如果我替换
sound.play(...)
为sound.beep
,我会得到一个不错的默认系统声音。因此,将资源路径传递给函数应该有问题。
如果有任何问题,我正在使用在线附加组件生成器。
请提出一个播放扩展音频资源的解决方案。