minimalpop
minimalpop

Reputation: 7058

Audio tag not working in firefox add-on

I have a FireFox extension where I am trying to play (and loop) an audio file when a specific page loads. I am embedding an audio tag like so:

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);
    });
};

and am passing a local audiofile using my main.js file:

main.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);
  }
});

I see the audio tag being inserted in the webpage, however it doesn't play.

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

When I go to the actual src link in the browser the file plays. Does anyone know why the file isn't playing?

Upvotes: 1

Views: 130

Answers (1)

Multimedia Mike
Multimedia Mike

Reputation: 13216

Perhaps you need to set the autoplay attribute on the audio tag:

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

Without that, an audio tag will just instantiate in a web page and sit there until the user asks it to start playing (assuming the controls attribute was specified).

Upvotes: 1

Related Questions