Created
March 16, 2015 08:05
-
-
Save evanxd/2e38bbb9a8c13e1f4717 to your computer and use it in GitHub Desktop.
Audio Channel Controller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* global BaseUI */ | |
'use strict'; | |
(function(exports) { | |
var AudioChannelController = function(app) { | |
this.app = app; | |
this.appId = app.instanceID; | |
this._audioChannels = new Map(); | |
app.browser.element.allowedAudioChannels.forEach((channel) => { | |
channel.addEventListener('activestatechanged', this); | |
this._audioChannels.set(channel.name, channel); | |
}); | |
return this; | |
}; | |
AudioChannelController.prototype = Object.create(BaseUI.prototype); | |
AudioChannelController.prototype.EVENT_PREFIX = 'audiochannel'; | |
AudioChannelController.prototype.handleEvent = function(evt) { | |
var channel = evt.target; | |
switch (evt.type) { | |
case 'activestatechanged': | |
// We should get the `isActive` stage from evt.target.isActive, | |
// and get channel from evt.target.channel. | |
channel.isActive().onsuccess = (evt) => { | |
this.publish('statechanged', { | |
audioChannelController: this, | |
channelName: channel.name, | |
isActive: evt.target.result | |
}); | |
}; | |
break; | |
} | |
}; | |
/** | |
* Play the audio channel. | |
* | |
* @param {BrowserElementAudioChannel} channel A audio channel. | |
* @param {Number} volume 0 to 1. | |
*/ | |
AudioChannelController.prototype.play = | |
function(channelName, volume, callback) { | |
var playRequest = this._audioChannels.get(channelName).setMuted(false); | |
playRequest.onsuccess = () => { | |
callback && callback(); | |
this.debug(channelName + ' is playing'); | |
}; | |
playRequest.onerror = () => { | |
this.debug('Error of playing ' + channelName); | |
}; | |
if (volume != null) { | |
this.setVolume(channelName, volume); | |
} | |
}; | |
/** | |
* Pause the audio channel. | |
* | |
* @param {BrowserElementAudioChannel} channel A audio channel. | |
*/ | |
AudioChannelController.prototype.pause = function(channelName, callback) { | |
var pauseRequest = this._audioChannels.get(channelName).setMuted(true); | |
pauseRequest.onsuccess = () => { | |
callback && callback(); | |
this.debug('Pause ' + channelName); | |
}; | |
pauseRequest.onerror = () => { | |
this.debug('Error of pausing the channel'); | |
}; | |
}; | |
/** | |
* Change volume of audio channels. | |
* | |
* @param {BrowserElementAudioChannel} channel. | |
* Audio channels you want to turn down or up its volume. | |
* @param {Number} volume 0 to 1. | |
*/ | |
AudioChannelController.prototype.setVolume = | |
function(channelName, volume, callback) { | |
var volumeRequest = this._audioChannels.get(channelName).setVolume(volume); | |
volumeRequest.onsuccess = () => { | |
callback && callback(); | |
this.debug(channelName + ' set volume: ' + volume); | |
}; | |
volumeRequest.onerror = () => { | |
this.debug('Error of setting volume'); | |
}; | |
}; | |
exports.AudioChannelController = AudioChannelController; | |
}(window)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment