从firefox扩展的数据目录播放音频

时间:2013-05-08 20:41:04

标签: javascript firefox firefox-addon firefox-addon-sdk

我正在制作Firefox扩展程序,但我无法播放位于附加组件data目录中的声音。

  1. 我尝试的第一件事就是以这种方式在内容脚本中播放:

    var soundFile = self.options.soundFile;
    (new Audio(soundFile)).play();
    

    其中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.
    
  2. 然后我找到了一种在main.js脚本中播放声音的方法(此处:How to play audio in an extension?):

    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,我会得到一个不错的默认系统声音。因此,将资源路径传递给函数应该有问题。

  3. 如果以任何方式重要,我正在使用在线附加组件生成器。

    请建议播放扩展音频资源解决方案的解决方案。

1 个答案:

答案 0 :(得分:7)

nsISound.play()需要nsIURI个实例,而不是字符串。您必须先通过nsIIOService.newURI()创建它:

var uri = Cc["@mozilla.org/network/io-service;1"]
            .getService(Ci.nsIIOService)
            .newURI(data.url('complete.oga'), null, null);
sound.play(uri);

我建议不要采用这种方法,nsISound.play() API pretty much deprecated,恕我直言,无论如何都无法播放ogg文件。

Audio构造函数需要访问适当的HTML文档。在基于SDK的扩展程序中获取一个扩展程序的最简单方法是使用page-worker module

require("page-worker").Page({
  contentScript: "new Audio('complete.oga').play()",
  contentURL: data.url("blank.html")
});

您的声音文件和blank.html(空HTML文件)都需要位于扩展程序的data/目录中 - 请注意音频文件是如何通过相对URL加载的。现在我没有办法避免在扩展程序中放置一个空的HTML文件,扩展程序外的HTML文件无法访问扩展文件而无法播放它们。