Skip to content

Instantly share code, notes, and snippets.

@evanxd
Last active August 29, 2015 14:09
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save evanxd/c5ee3c5b8a389e01227a to your computer and use it in GitHub Desktop.
Audio Channel Manager
/**
* Use audio channel manager in System app[1].
*
* AppWindowManager.prototype.handleEvent = function(evt) {
* switch (evt.type) {
* case 'appcreated':
* var app = evt.detail;
* this._apps[evt.detail.instanceID] = app;
* audioChannelManager.register(app.browser.element);
* break;
* }
* };
*
* [1] https://github.com/mozilla-b2g/gaia/blob/master/apps/system/js/app_window_manager.js#L496
*/
function AudioChannelManager() {
this._playingApps = [];
this._pausedApps = [];
}
AudioChannelManager.prototype = {
// A list of mozBrowser elements.
_playingApps: null,
_pausedApps: null,
_competingResults: {
PLAY: 0,
VIBRATE: 1,
SMALLER_VOLUME_FOR_NEW_CHANNEL: 2,
SMALLER_VOLUME_FOR_CURRENT_CHANNEL: 3,
PAUSE_THEN_RESUME_CURRENTCHANNEL: 4
},
register: function(app) {
app.addEventListener('mozaudiochannelchange', function(evt) {
switch (evt.detail.state) {
case 'play':
this._handleAudioChannelCompeting(evt.detail.channel);
break;
case 'end':
if (this._pausedApps.length) {
this._resumePausedAudioChannel();
}
break;
}
});
},
/**
* Do we need `unregister` method to remove the listener
* when app is killed?
*/
unregister: function() {},
/**
* Implement the table of audio channel competing.
*/
_handleAudioChannelCompeting: function(channel) {
var competingResults = this._competingResults;
switch (this.compete(channel)) {
case competingResults.PLAY:
this._play(app, channel);
break;
case competingResults.VIBRATE:
this._vibrate(app);
break;
case competingResults.SMALLER_VOLUME_FOR_NEW_CHANNEL:
this._play(app, channel, 0.2);
break;
case competingResults.SMALLER_VOLUME_FOR_CURRENT_CHANNEL:
_playingApps.forEach(function(app) {
Array.prototype.forEach.call(app.allowedAudioChannels, function(channel) {
this._play(app, channel, 0.2);
});
});
break;
case competingResults.PAUSE_THEN_RESUME_CURRENTCHANNEL:
this._pause(app, channel);
break;
}
},
_compete: function(newAudioChannel) {
// Compete with current playing channel.
return resultCode;
},
_resumePausedAudioChannel: function() {},
_play: function(app, audioChannel, volume) {},
_vibrate: function(app) {},
_pause: function(app, channel) {},
_pauseThenResume: function(app1, app2, audioChannel1, audioChannel2) {}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment