Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to play audio in an extension?

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.

like image 534
DoTheEvo Avatar asked May 19 '12 01:05

DoTheEvo


People also ask

What is Media Player extension?

The "Media Player" is a browser extension to play music and video files either locally or from online sources. This project aims to bring a VLC-liked media playing experience using native HTML5 technology.

Is there a Chrome extension for music?

Audio Channel is a holistic Chrome extension that lets you customize all aspects of your browser's musical output. There's a volume control (which offers up to 400 percent boost), an audio compressor, a 32Hz-16kHz equalizer, a stereo/mono toggle, and special effects including chorus, reverb, and pitch shift.


2 Answers

var window = require("sdk/window/utils").getMostRecentBrowserWindow();
AudioObj = new window.Audio;
like image 38
jmjm Avatar answered Oct 22 '22 22:10

jmjm


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.

like image 116
Wladimir Palant Avatar answered Oct 22 '22 23:10

Wladimir Palant