March 11, 2014
Audio volume and mute per window object

I have finally found some time to finish a set of patches about a nice feature that will allow addon/firefox developers to control audio volumes for window object.

Through this new feature any window object has two new attributes: audioMuted and audioVolume (accessible from chrome code only using nsIDOMWindowUtils). The aim is to change the volume of any HTML5 media element and any WebAudio destination node (soon WebRTC and also FMRadio API). The control of the volumes works “on cascade” - if a window has an iframe, the iframe elements will be effected by the parent window audio attributes.

The code is just landed on m-c and it will be available on nightly in a few hours.

Also, in order to test this feature I wrote an addon. As you could see, the UI is not the best… I know, but it was just a proof of concept, I’m sure somebody else will do a better work! Download the addon.

This feature is currently disabled by default, but it’s easy to enable it by changing or creating a preference in about:config. Some instructions to do it: open ‘about:config’ in a new tab and add a new boolean preference called ‘media.useAudioChannelService' and set it to true. This property will enable the AudioChannelService for any HTMLMediaElement and any WebAudio destination node.

AudioChannelService is the audio policy controller of Firefox OS. You will know once you use it, that the AudioChannelService is enabled when, while changing tabs, media elements of invisible tabs will be muted. From now on, you can use the addon.

The addon UI can be open from Tools -> Web Developers -> Audio Test.

Here a screenshot:

AudioTest addon

From a code point of view, you can play with this audio feature from the nsIDOMWindowUtils interface. For instance:

var currentBrowser = tabbrowser.getBrowserAtIndex(0 /* an index */);
var utils = currentBrowser.contentWindow
                          .QueryInterface(Ci.nsIInterfaceRequestor)
                          .getInterface(Ci.nsIDOMWindowUtils);
dump("The default audioVolume should be 1.0: " + utils.audioVolume + "\n");
utils.audioVolume = 0.8;
dump("By default the window is not muted: " + utils.audioMuted + "\n");
utils.audioMuted = true;

(More info about tabbrowser object can be read from this page)

There is also a notification that is dispatched when a window starts and stops playing audio: media-playback. The value of this notification can be active or inactive. If you are interested about how to use them, there are a couple of tests on bug 923247.

What can we do with this new feature? Here some ideas:

  • we could change or animate the tab icon to let the users know which tab is playing.
  • addons to mix audio between tabs
  • advanced settings for disable audio by default - either to enable audio just for the visible tab or to specific origins, etc.
  • other options, such as “disable audio for all the tabs but this one” in some context menu.

Please, use this feature and let me know if you have problems, new ideas or needs. Good hack!

  1. 143th posted this