【问题标题】:Audio tag not working in firefox add-on音频标签在 Firefox 插件中不起作用
【发布时间】:2013-06-06 19:06:51
【问题描述】:

我有一个 FireFox 扩展,我试图在加载特定页面时播放(和循环播放)音频文件。我正在嵌入这样的音频标签:

start.js

window.onload = function() {
    self.port.on("soundfile", function(soundurl){
        var audioElement = document.createElement('audio');
        audioElement.setAttribute('src', soundurl);
        audioElement.load();
        audioElement.addEventListener("load", function() { 
            audioElement.play(); 
            audioElement.loop();
        }, true);
        document.body.insertBefore(audioElement, document.body.firstChild);
    });
};

并且正在使用我的 main.js 文件传递​​本地音频文件:

ma​​in.js

var pageMod = require("sdk/page-mod");
var self = require("sdk/self");
var soundurl = self.data.url("sample.ogg");

pageMod.PageMod({
  include: "pandasarethebe.st",
  contentScriptFile: self.data.url("start.js"),
  onAttach: function(worker) {
      worker.port.emit("soundfile",soundurl);
  }
});

我看到网页中插入了音频标签,但它无法播放。

<audio src="resource://jid1-zlxnevw93j6qaa-at-jetpack/sampleapp/data/sample.ogg"></audio>

当我转到浏览器中的实际 src 链接时,文件会播放。有谁知道为什么文件没有播放?

【问题讨论】:

    标签: javascript html firefox audio firefox-addon


    【解决方案1】:

    也许你需要在音频标签上设置自动播放属性:

    <audio autoplay="autoplay" src="whatever.ogg" />
    

    如果不这样做,音频标签只会在网页中实例化并坐在那里,直到用户要求它开始播放(假设指定了 controls 属性)。

    【讨论】:

      猜你喜欢
      • 2012-10-24
      • 2011-11-13
      • 2021-11-27
      • 2012-08-21
      • 1970-01-01
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      • 2013-07-27
      相关资源
      最近更新 更多