audios
Version:
Stateful react components for audio playback on the web
1,565 lines (1,268 loc) • 119 kB
JavaScript
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
var React = require('react');
var React__default = _interopDefault(React);
var PropTypes = _interopDefault(require('prop-types'));
var commonjsGlobal = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
function unwrapExports (x) {
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
}
function createCommonjsModule(fn, module) {
return module = { exports: {} }, fn(module, module.exports), module.exports;
}
var lib = createCommonjsModule(function (module, exports) {
exports.__esModule = true;
var _react2 = _interopRequireDefault(React__default);
var _propTypes2 = _interopRequireDefault(PropTypes);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
function createEventEmitter(value) {
var handlers = [];
return {
on: function on(handler) {
handlers.push(handler);
},
off: function off(handler) {
handlers = handlers.filter(function (h) {
return h !== handler;
});
},
get: function get() {
return value;
},
set: function set(newValue) {
value = newValue;
handlers.forEach(function (handler) {
return handler(value);
});
}
};
}
function onlyChild(children) {
return Array.isArray(children) ? children[0] : children;
}
var uniqueId = 0;
function createReactContext(defaultValue) {
var _Provider$childContex, _Consumer$contextType;
var contextProp = '__create-react-context-' + uniqueId++ + '__';
var Provider = function (_Component) {
_inherits(Provider, _Component);
function Provider() {
var _temp, _this, _ret;
_classCallCheck(this, Provider);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _ret = (_temp = (_this = _possibleConstructorReturn(this, _Component.call.apply(_Component, [this].concat(args))), _this), _this.emitter = createEventEmitter(_this.props.value), _temp), _possibleConstructorReturn(_this, _ret);
}
Provider.prototype.getChildContext = function getChildContext() {
var _ref;
return _ref = {}, _ref[contextProp] = this.emitter, _ref;
};
Provider.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
if (this.props.value !== nextProps.value) {
this.emitter.set(nextProps.value);
}
};
Provider.prototype.render = function render() {
return this.props.children;
};
return Provider;
}(React__default.Component);
Provider.childContextTypes = (_Provider$childContex = {}, _Provider$childContex[contextProp] = _propTypes2.default.object.isRequired, _Provider$childContex);
var Consumer = function (_Component2) {
_inherits(Consumer, _Component2);
function Consumer() {
var _temp2, _this2, _ret2;
_classCallCheck(this, Consumer);
for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
args[_key2] = arguments[_key2];
}
return _ret2 = (_temp2 = (_this2 = _possibleConstructorReturn(this, _Component2.call.apply(_Component2, [this].concat(args))), _this2), _this2.state = {
value: _this2.getValue()
}, _this2.onUpdate = function () {
_this2.setState({
value: _this2.getValue()
});
}, _temp2), _possibleConstructorReturn(_this2, _ret2);
}
Consumer.prototype.componentDidMount = function componentDidMount() {
if (this.context[contextProp]) {
this.context[contextProp].on(this.onUpdate);
}
};
Consumer.prototype.componentWillUnmount = function componentWillUnmount() {
if (this.context[contextProp]) {
this.context[contextProp].off(this.onUpdate);
}
};
Consumer.prototype.getValue = function getValue() {
if (this.context[contextProp]) {
return this.context[contextProp].get();
} else {
return defaultValue;
}
};
Consumer.prototype.render = function render() {
return onlyChild(this.props.children)(this.state.value);
};
return Consumer;
}(React__default.Component);
Consumer.contextTypes = (_Consumer$contextType = {}, _Consumer$contextType[contextProp] = _propTypes2.default.object, _Consumer$contextType);
return {
Provider: Provider,
Consumer: Consumer
};
}
exports.default = createReactContext;
module.exports = exports['default'];
});
var createReactContext = unwrapExports(lib);
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var StateContext = createReactContext(null);
var Container = function () {
function Container() {
var _this = this;
_classCallCheck(this, Container);
this._listeners = [];
CONTAINER_DEBUG_CALLBACKS.forEach(function (cb) {
return cb(_this);
});
}
Container.prototype.setState = function setState(updater, callback) {
var _this2 = this;
return Promise.resolve().then(function () {
var nextState = void 0;
if (typeof updater === 'function') {
nextState = updater(_this2.state);
} else {
nextState = updater;
}
if (nextState == null) {
if (callback) callback();
return;
}
_this2.state = Object.assign({}, _this2.state, nextState);
var promises = _this2._listeners.map(function (listener) {
return listener();
});
return Promise.all(promises).then(function () {
if (callback) {
return callback();
}
});
});
};
Container.prototype.subscribe = function subscribe(fn) {
this._listeners.push(fn);
};
Container.prototype.unsubscribe = function unsubscribe(fn) {
this._listeners = this._listeners.filter(function (f) {
return f !== fn;
});
};
return Container;
}();
var DUMMY_STATE = {};
var Subscribe = function (_React$Component) {
_inherits(Subscribe, _React$Component);
function Subscribe() {
var _temp, _this3, _ret;
_classCallCheck(this, Subscribe);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _ret = (_temp = (_this3 = _possibleConstructorReturn(this, _React$Component.call.apply(_React$Component, [this].concat(args))), _this3), _this3.state = {}, _this3.instances = [], _this3.unmounted = false, _this3.onUpdate = function () {
return new Promise(function (resolve) {
if (!_this3.unmounted) {
_this3.setState(DUMMY_STATE, resolve);
} else {
resolve();
}
});
}, _temp), _possibleConstructorReturn(_this3, _ret);
}
Subscribe.prototype.componentWillUnmount = function componentWillUnmount() {
this.unmounted = true;
this._unsubscribe();
};
Subscribe.prototype._unsubscribe = function _unsubscribe() {
var _this4 = this;
this.instances.forEach(function (container) {
container.unsubscribe(_this4.onUpdate);
});
};
Subscribe.prototype._createInstances = function _createInstances(map, containers) {
var _this5 = this;
this._unsubscribe();
if (map === null) {
throw new Error('You must wrap your <Subscribe> components with a <Provider>');
}
var safeMap = map;
var instances = containers.map(function (ContainerItem) {
var instance = void 0;
if ((typeof ContainerItem === 'undefined' ? 'undefined' : _typeof(ContainerItem)) === 'object' && ContainerItem instanceof Container) {
instance = ContainerItem;
} else {
instance = safeMap.get(ContainerItem);
if (!instance) {
instance = new ContainerItem();
safeMap.set(ContainerItem, instance);
}
}
instance.unsubscribe(_this5.onUpdate);
instance.subscribe(_this5.onUpdate);
return instance;
});
this.instances = instances;
return instances;
};
Subscribe.prototype.render = function render() {
var _this6 = this;
return React__default.createElement(
StateContext.Consumer,
null,
function (map) {
return _this6.props.children.apply(null, _this6._createInstances(map, _this6.props.to));
}
);
};
return Subscribe;
}(React__default.Component);
function Provider(props) {
return React__default.createElement(
StateContext.Consumer,
null,
function (parentMap) {
var childMap = new Map(parentMap);
if (props.inject) {
props.inject.forEach(function (instance) {
childMap.set(instance.constructor, instance);
});
}
return React__default.createElement(
StateContext.Provider,
{ value: childMap },
props.children
);
}
);
}
var CONTAINER_DEBUG_CALLBACKS = [];
var classCallCheck = function (instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
};
var createClass = function () {
function defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
return function (Constructor, protoProps, staticProps) {
if (protoProps) defineProperties(Constructor.prototype, protoProps);
if (staticProps) defineProperties(Constructor, staticProps);
return Constructor;
};
}();
var _extends = Object.assign || function (target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i];
for (var key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
target[key] = source[key];
}
}
}
return target;
};
var inherits = function (subClass, superClass) {
if (typeof superClass !== "function" && superClass !== null) {
throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
}
subClass.prototype = Object.create(superClass && superClass.prototype, {
constructor: {
value: subClass,
enumerable: false,
writable: true,
configurable: true
}
});
if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
};
var possibleConstructorReturn = function (self, call) {
if (!self) {
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
}
return call && (typeof call === "object" || typeof call === "function") ? call : self;
};
var Audios = function (_Component) {
inherits(Audios, _Component);
function Audios() {
classCallCheck(this, Audios);
return possibleConstructorReturn(this, (Audios.__proto__ || Object.getPrototypeOf(Audios)).apply(this, arguments));
}
createClass(Audios, [{
key: 'render',
value: function render() {
var children = this.props.children;
return React__default.createElement(
Provider,
null,
children
);
}
}]);
return Audios;
}(React.Component);
Audios.propTypes = {
children: PropTypes.node
};
var howler = createCommonjsModule(function (module, exports) {
/*!
* howler.js v2.0.15
* howlerjs.com
*
* (c) 2013-2018, James Simpson of GoldFire Studios
* goldfirestudios.com
*
* MIT License
*/
(function() {
/** Global Methods **/
/***************************************************************************/
/**
* Create the global controller. All contained methods and properties apply
* to all sounds that are currently playing or will be in the future.
*/
var HowlerGlobal = function() {
this.init();
};
HowlerGlobal.prototype = {
/**
* Initialize the global Howler object.
* @return {Howler}
*/
init: function() {
var self = this || Howler;
// Create a global ID counter.
self._counter = 1000;
// Internal properties.
self._codecs = {};
self._howls = [];
self._muted = false;
self._volume = 1;
self._canPlayEvent = 'canplaythrough';
self._navigator = (typeof window !== 'undefined' && window.navigator) ? window.navigator : null;
// Public properties.
self.masterGain = null;
self.noAudio = false;
self.usingWebAudio = true;
self.autoSuspend = true;
self.ctx = null;
// Set to false to disable the auto iOS enabler.
self.mobileAutoEnable = true;
// Setup the various state values for global tracking.
self._setup();
return self;
},
/**
* Get/set the global volume for all sounds.
* @param {Float} vol Volume from 0.0 to 1.0.
* @return {Howler/Float} Returns self or current volume.
*/
volume: function(vol) {
var self = this || Howler;
vol = parseFloat(vol);
// If we don't have an AudioContext created yet, run the setup.
if (!self.ctx) {
setupAudioContext();
}
if (typeof vol !== 'undefined' && vol >= 0 && vol <= 1) {
self._volume = vol;
// Don't update any of the nodes if we are muted.
if (self._muted) {
return self;
}
// When using Web Audio, we just need to adjust the master gain.
if (self.usingWebAudio) {
self.masterGain.gain.setValueAtTime(vol, Howler.ctx.currentTime);
}
// Loop through and change volume for all HTML5 audio nodes.
for (var i=0; i<self._howls.length; i++) {
if (!self._howls[i]._webAudio) {
// Get all of the sounds in this Howl group.
var ids = self._howls[i]._getSoundIds();
// Loop through all sounds and change the volumes.
for (var j=0; j<ids.length; j++) {
var sound = self._howls[i]._soundById(ids[j]);
if (sound && sound._node) {
sound._node.volume = sound._volume * vol;
}
}
}
}
return self;
}
return self._volume;
},
/**
* Handle muting and unmuting globally.
* @param {Boolean} muted Is muted or not.
*/
mute: function(muted) {
var self = this || Howler;
// If we don't have an AudioContext created yet, run the setup.
if (!self.ctx) {
setupAudioContext();
}
self._muted = muted;
// With Web Audio, we just need to mute the master gain.
if (self.usingWebAudio) {
self.masterGain.gain.setValueAtTime(muted ? 0 : self._volume, Howler.ctx.currentTime);
}
// Loop through and mute all HTML5 Audio nodes.
for (var i=0; i<self._howls.length; i++) {
if (!self._howls[i]._webAudio) {
// Get all of the sounds in this Howl group.
var ids = self._howls[i]._getSoundIds();
// Loop through all sounds and mark the audio node as muted.
for (var j=0; j<ids.length; j++) {
var sound = self._howls[i]._soundById(ids[j]);
if (sound && sound._node) {
sound._node.muted = (muted) ? true : sound._muted;
}
}
}
}
return self;
},
/**
* Unload and destroy all currently loaded Howl objects.
* @return {Howler}
*/
unload: function() {
var self = this || Howler;
for (var i=self._howls.length-1; i>=0; i--) {
self._howls[i].unload();
}
// Create a new AudioContext to make sure it is fully reset.
if (self.usingWebAudio && self.ctx && typeof self.ctx.close !== 'undefined') {
self.ctx.close();
self.ctx = null;
setupAudioContext();
}
return self;
},
/**
* Check for codec support of specific extension.
* @param {String} ext Audio file extention.
* @return {Boolean}
*/
codecs: function(ext) {
return (this || Howler)._codecs[ext.replace(/^x-/, '')];
},
/**
* Setup various state values for global tracking.
* @return {Howler}
*/
_setup: function() {
var self = this || Howler;
// Keeps track of the suspend/resume state of the AudioContext.
self.state = self.ctx ? self.ctx.state || 'running' : 'running';
// Automatically begin the 30-second suspend process
self._autoSuspend();
// Check if audio is available.
if (!self.usingWebAudio) {
// No audio is available on this system if noAudio is set to true.
if (typeof Audio !== 'undefined') {
try {
var test = new Audio();
// Check if the canplaythrough event is available.
if (typeof test.oncanplaythrough === 'undefined') {
self._canPlayEvent = 'canplay';
}
} catch(e) {
self.noAudio = true;
}
} else {
self.noAudio = true;
}
}
// Test to make sure audio isn't disabled in Internet Explorer.
try {
var test = new Audio();
if (test.muted) {
self.noAudio = true;
}
} catch (e) {}
// Check for supported codecs.
if (!self.noAudio) {
self._setupCodecs();
}
return self;
},
/**
* Check for browser support for various codecs and cache the results.
* @return {Howler}
*/
_setupCodecs: function() {
var self = this || Howler;
var audioTest = null;
// Must wrap in a try/catch because IE11 in server mode throws an error.
try {
audioTest = (typeof Audio !== 'undefined') ? new Audio() : null;
} catch (err) {
return self;
}
if (!audioTest || typeof audioTest.canPlayType !== 'function') {
return self;
}
var mpegTest = audioTest.canPlayType('audio/mpeg;').replace(/^no$/, '');
// Opera version <33 has mixed MP3 support, so we need to check for and block it.
var checkOpera = self._navigator && self._navigator.userAgent.match(/OPR\/([0-6].)/g);
var isOldOpera = (checkOpera && parseInt(checkOpera[0].split('/')[1], 10) < 33);
self._codecs = {
mp3: !!(!isOldOpera && (mpegTest || audioTest.canPlayType('audio/mp3;').replace(/^no$/, ''))),
mpeg: !!mpegTest,
opus: !!audioTest.canPlayType('audio/ogg; codecs="opus"').replace(/^no$/, ''),
ogg: !!audioTest.canPlayType('audio/ogg; codecs="vorbis"').replace(/^no$/, ''),
oga: !!audioTest.canPlayType('audio/ogg; codecs="vorbis"').replace(/^no$/, ''),
wav: !!audioTest.canPlayType('audio/wav; codecs="1"').replace(/^no$/, ''),
aac: !!audioTest.canPlayType('audio/aac;').replace(/^no$/, ''),
caf: !!audioTest.canPlayType('audio/x-caf;').replace(/^no$/, ''),
m4a: !!(audioTest.canPlayType('audio/x-m4a;') || audioTest.canPlayType('audio/m4a;') || audioTest.canPlayType('audio/aac;')).replace(/^no$/, ''),
mp4: !!(audioTest.canPlayType('audio/x-mp4;') || audioTest.canPlayType('audio/mp4;') || audioTest.canPlayType('audio/aac;')).replace(/^no$/, ''),
weba: !!audioTest.canPlayType('audio/webm; codecs="vorbis"').replace(/^no$/, ''),
webm: !!audioTest.canPlayType('audio/webm; codecs="vorbis"').replace(/^no$/, ''),
dolby: !!audioTest.canPlayType('audio/mp4; codecs="ec-3"').replace(/^no$/, ''),
flac: !!(audioTest.canPlayType('audio/x-flac;') || audioTest.canPlayType('audio/flac;')).replace(/^no$/, '')
};
return self;
},
/**
* Mobile browsers will only allow audio to be played after a user interaction.
* Attempt to automatically unlock audio on the first user interaction.
* Concept from: http://paulbakaus.com/tutorials/html5/web-audio-on-ios/
* @return {Howler}
*/
_enableMobileAudio: function() {
var self = this || Howler;
// Only run this on mobile devices if audio isn't already eanbled.
var isMobile = /iPhone|iPad|iPod|Android|BlackBerry|BB10|Silk|Mobi|Chrome/i.test(self._navigator && self._navigator.userAgent);
if (self._mobileEnabled || !self.ctx || !isMobile) {
return;
}
self._mobileEnabled = false;
self.mobileAutoEnable = false;
// Some mobile devices/platforms have distortion issues when opening/closing tabs and/or web views.
// Bugs in the browser (especially Mobile Safari) can cause the sampleRate to change from 44100 to 48000.
// By calling Howler.unload(), we create a new AudioContext with the correct sampleRate.
if (!self._mobileUnloaded && self.ctx.sampleRate !== 44100) {
self._mobileUnloaded = true;
self.unload();
}
// Scratch buffer for enabling iOS to dispose of web audio buffers correctly, as per:
// http://stackoverflow.com/questions/24119684
self._scratchBuffer = self.ctx.createBuffer(1, 1, 22050);
// Call this method on touch start to create and play a buffer,
// then check if the audio actually played to determine if
// audio has now been unlocked on iOS, Android, etc.
var unlock = function(e) {
// Fix Android can not play in suspend state.
Howler._autoResume();
// Create an empty buffer.
var source = self.ctx.createBufferSource();
source.buffer = self._scratchBuffer;
source.connect(self.ctx.destination);
// Play the empty buffer.
if (typeof source.start === 'undefined') {
source.noteOn(0);
} else {
source.start(0);
}
// Calling resume() on a stack initiated by user gesture is what actually unlocks the audio on Android Chrome >= 55.
if (typeof self.ctx.resume === 'function') {
self.ctx.resume();
}
// Setup a timeout to check that we are unlocked on the next event loop.
source.onended = function() {
source.disconnect(0);
// Update the unlocked state and prevent this check from happening again.
self._mobileEnabled = true;
// Remove the touch start listener.
document.removeEventListener('touchstart', unlock, true);
document.removeEventListener('touchend', unlock, true);
document.removeEventListener('click', unlock, true);
// Let all sounds know that audio has been unlocked.
for (var i=0; i<self._howls.length; i++) {
self._howls[i]._emit('unlock');
}
};
};
// Setup a touch start listener to attempt an unlock in.
document.addEventListener('touchstart', unlock, true);
document.addEventListener('touchend', unlock, true);
document.addEventListener('click', unlock, true);
return self;
},
/**
* Automatically suspend the Web Audio AudioContext after no sound has played for 30 seconds.
* This saves processing/energy and fixes various browser-specific bugs with audio getting stuck.
* @return {Howler}
*/
_autoSuspend: function() {
var self = this;
if (!self.autoSuspend || !self.ctx || typeof self.ctx.suspend === 'undefined' || !Howler.usingWebAudio) {
return;
}
// Check if any sounds are playing.
for (var i=0; i<self._howls.length; i++) {
if (self._howls[i]._webAudio) {
for (var j=0; j<self._howls[i]._sounds.length; j++) {
if (!self._howls[i]._sounds[j]._paused) {
return self;
}
}
}
}
if (self._suspendTimer) {
clearTimeout(self._suspendTimer);
}
// If no sound has played after 30 seconds, suspend the context.
self._suspendTimer = setTimeout(function() {
if (!self.autoSuspend) {
return;
}
self._suspendTimer = null;
self.state = 'suspending';
self.ctx.suspend().then(function() {
self.state = 'suspended';
if (self._resumeAfterSuspend) {
delete self._resumeAfterSuspend;
self._autoResume();
}
});
}, 30000);
return self;
},
/**
* Automatically resume the Web Audio AudioContext when a new sound is played.
* @return {Howler}
*/
_autoResume: function() {
var self = this;
if (!self.ctx || typeof self.ctx.resume === 'undefined' || !Howler.usingWebAudio) {
return;
}
if (self.state === 'running' && self._suspendTimer) {
clearTimeout(self._suspendTimer);
self._suspendTimer = null;
} else if (self.state === 'suspended') {
self.ctx.resume().then(function() {
self.state = 'running';
// Emit to all Howls that the audio has resumed.
for (var i=0; i<self._howls.length; i++) {
self._howls[i]._emit('resume');
}
});
if (self._suspendTimer) {
clearTimeout(self._suspendTimer);
self._suspendTimer = null;
}
} else if (self.state === 'suspending') {
self._resumeAfterSuspend = true;
}
return self;
}
};
// Setup the global audio controller.
var Howler = new HowlerGlobal();
/** Group Methods **/
/***************************************************************************/
/**
* Create an audio group controller.
* @param {Object} o Passed in properties for this group.
*/
var Howl = function(o) {
var self = this;
// Throw an error if no source is provided.
if (!o.src || o.src.length === 0) {
console.error('An array of source files must be passed with any new Howl.');
return;
}
self.init(o);
};
Howl.prototype = {
/**
* Initialize a new Howl group object.
* @param {Object} o Passed in properties for this group.
* @return {Howl}
*/
init: function(o) {
var self = this;
// If we don't have an AudioContext created yet, run the setup.
if (!Howler.ctx) {
setupAudioContext();
}
// Setup user-defined default properties.
self._autoplay = o.autoplay || false;
self._format = (typeof o.format !== 'string') ? o.format : [o.format];
self._html5 = o.html5 || false;
self._muted = o.mute || false;
self._loop = o.loop || false;
self._pool = o.pool || 5;
self._preload = (typeof o.preload === 'boolean') ? o.preload : true;
self._rate = o.rate || 1;
self._sprite = o.sprite || {};
self._src = (typeof o.src !== 'string') ? o.src : [o.src];
self._volume = o.volume !== undefined ? o.volume : 1;
self._xhrWithCredentials = o.xhrWithCredentials || false;
// Setup all other default properties.
self._duration = 0;
self._state = 'unloaded';
self._sounds = [];
self._endTimers = {};
self._queue = [];
self._playLock = false;
// Setup event listeners.
self._onend = o.onend ? [{fn: o.onend}] : [];
self._onfade = o.onfade ? [{fn: o.onfade}] : [];
self._onload = o.onload ? [{fn: o.onload}] : [];
self._onloaderror = o.onloaderror ? [{fn: o.onloaderror}] : [];
self._onplayerror = o.onplayerror ? [{fn: o.onplayerror}] : [];
self._onpause = o.onpause ? [{fn: o.onpause}] : [];
self._onplay = o.onplay ? [{fn: o.onplay}] : [];
self._onstop = o.onstop ? [{fn: o.onstop}] : [];
self._onmute = o.onmute ? [{fn: o.onmute}] : [];
self._onvolume = o.onvolume ? [{fn: o.onvolume}] : [];
self._onrate = o.onrate ? [{fn: o.onrate}] : [];
self._onseek = o.onseek ? [{fn: o.onseek}] : [];
self._onunlock = o.onunlock ? [{fn: o.onunlock}] : [];
self._onresume = [];
// Web Audio or HTML5 Audio?
self._webAudio = Howler.usingWebAudio && !self._html5;
// Automatically try to enable audio on iOS.
if (typeof Howler.ctx !== 'undefined' && Howler.ctx && Howler.mobileAutoEnable) {
Howler._enableMobileAudio();
}
// Keep track of this Howl group in the global controller.
Howler._howls.push(self);
// If they selected autoplay, add a play event to the load queue.
if (self._autoplay) {
self._queue.push({
event: 'play',
action: function() {
self.play();
}
});
}
// Load the source file unless otherwise specified.
if (self._preload) {
self.load();
}
return self;
},
/**
* Load the audio file.
* @return {Howler}
*/
load: function() {
var self = this;
var url = null;
// If no audio is available, quit immediately.
if (Howler.noAudio) {
self._emit('loaderror', null, 'No audio support.');
return;
}
// Make sure our source is in an array.
if (typeof self._src === 'string') {
self._src = [self._src];
}
// Loop through the sources and pick the first one that is compatible.
for (var i=0; i<self._src.length; i++) {
var ext, str;
if (self._format && self._format[i]) {
// If an extension was specified, use that instead.
ext = self._format[i];
} else {
// Make sure the source is a string.
str = self._src[i];
if (typeof str !== 'string') {
self._emit('loaderror', null, 'Non-string found in selected audio sources - ignoring.');
continue;
}
// Extract the file extension from the URL or base64 data URI.
ext = /^data:audio\/([^;,]+);/i.exec(str);
if (!ext) {
ext = /\.([^.]+)$/.exec(str.split('?', 1)[0]);
}
if (ext) {
ext = ext[1].toLowerCase();
}
}
// Log a warning if no extension was found.
if (!ext) {
console.warn('No file extension was found. Consider using the "format" property or specify an extension.');
}
// Check if this extension is available.
if (ext && Howler.codecs(ext)) {
url = self._src[i];
break;
}
}
if (!url) {
self._emit('loaderror', null, 'No codec support for selected audio sources.');
return;
}
self._src = url;
self._state = 'loading';
// If the hosting page is HTTPS and the source isn't,
// drop down to HTML5 Audio to avoid Mixed Content errors.
if (window.location.protocol === 'https:' && url.slice(0, 5) === 'http:') {
self._html5 = true;
self._webAudio = false;
}
// Create a new sound object and add it to the pool.
new Sound(self);
// Load and decode the audio data for playback.
if (self._webAudio) {
loadBuffer(self);
}
return self;
},
/**
* Play a sound or resume previous playback.
* @param {String/Number} sprite Sprite name for sprite playback or sound id to continue previous.
* @param {Boolean} internal Internal Use: true prevents event firing.
* @return {Number} Sound ID.
*/
play: function(sprite, internal) {
var self = this;
var id = null;
// Determine if a sprite, sound id or nothing was passed
if (typeof sprite === 'number') {
id = sprite;
sprite = null;
} else if (typeof sprite === 'string' && self._state === 'loaded' && !self._sprite[sprite]) {
// If the passed sprite doesn't exist, do nothing.
return null;
} else if (typeof sprite === 'undefined') {
// Use the default sound sprite (plays the full audio length).
sprite = '__default';
// Check if there is a single paused sound that isn't ended.
// If there is, play that sound. If not, continue as usual.
var num = 0;
for (var i=0; i<self._sounds.length; i++) {
if (self._sounds[i]._paused && !self._sounds[i]._ended) {
num++;
id = self._sounds[i]._id;
}
}
if (num === 1) {
sprite = null;
} else {
id = null;
}
}
// Get the selected node, or get one from the pool.
var sound = id ? self._soundById(id) : self._inactiveSound();
// If the sound doesn't exist, do nothing.
if (!sound) {
return null;
}
// Select the sprite definition.
if (id && !sprite) {
sprite = sound._sprite || '__default';
}
// If the sound hasn't loaded, we must wait to get the audio's duration.
// We also need to wait to make sure we don't run into race conditions with
// the order of function calls.
if (self._state !== 'loaded') {
// Set the sprite value on this sound.
sound._sprite = sprite;
// Makr this sounded as not ended in case another sound is played before this one loads.
sound._ended = false;
// Add the sound to the queue to be played on load.
var soundId = sound._id;
self._queue.push({
event: 'play',
action: function() {
self.play(soundId);
}
});
return soundId;
}
// Don't play the sound if an id was passed and it is already playing.
if (id && !sound._paused) {
// Trigger the play event, in order to keep iterating through queue.
if (!internal) {
self._loadQueue('play');
}
return sound._id;
}
// Make sure the AudioContext isn't suspended, and resume it if it is.
if (self._webAudio) {
Howler._autoResume();
}
// Determine how long to play for and where to start playing.
var seek = Math.max(0, sound._seek > 0 ? sound._seek : self._sprite[sprite][0] / 1000);
var duration = Math.max(0, ((self._sprite[sprite][0] + self._sprite[sprite][1]) / 1000) - seek);
var timeout = (duration * 1000) / Math.abs(sound._rate);
// Update the parameters of the sound
sound._paused = false;
sound._ended = false;
sound._sprite = sprite;
sound._seek = seek;
sound._start = self._sprite[sprite][0] / 1000;
sound._stop = (self._sprite[sprite][0] + self._sprite[sprite][1]) / 1000;
sound._loop = !!(sound._loop || self._sprite[sprite][2]);
// End the sound instantly if seek is at the end.
if (sound._seek >= sound._stop) {
self._ended(sound);
return;
}
// Begin the actual playback.
var node = sound._node;
if (self._webAudio) {
// Fire this when the sound is ready to play to begin Web Audio playback.
var playWebAudio = function() {
self._refreshBuffer(sound);
// Setup the playback params.
var vol = (sound._muted || self._muted) ? 0 : sound._volume;
node.gain.setValueAtTime(vol, Howler.ctx.currentTime);
sound._playStart = Howler.ctx.currentTime;
// Play the sound using the supported method.
if (typeof node.bufferSource.start === 'undefined') {
sound._loop ? node.bufferSource.noteGrainOn(0, seek, 86400) : node.bufferSource.noteGrainOn(0, seek, duration);
} else {
sound._loop ? node.bufferSource.start(0, seek, 86400) : node.bufferSource.start(0, seek, duration);
}
// Start a new timer if none is present.
if (timeout !== Infinity) {
self._endTimers[sound._id] = setTimeout(self._ended.bind(self, sound), timeout);
}
if (!internal) {
setTimeout(function() {
self._emit('play', sound._id);
}, 0);
}
};
if (Howler.state === 'running') {
playWebAudio();
} else {
self.once('resume', playWebAudio);
// Cancel the end timer.
self._clearTimer(sound._id);
}
} else {
// Fire this when the sound is ready to play to begin HTML5 Audio playback.
var playHtml5 = function() {
node.currentTime = seek;
node.muted = sound._muted || self._muted || Howler._muted || node.muted;
node.volume = sound._volume * Howler.volume();
node.playbackRate = sound._rate;
// Mobile browsers will throw an error if this is called without user interaction.
try {
var play = node.play();
// Support older browsers that don't support promises, and thus don't have this issue.
if (play && typeof Promise !== 'undefined' && (play instanceof Promise || typeof play.then === 'function')) {
// Implements a lock to prevent DOMException: The play() request was interrupted by a call to pause().
self._playLock = true;
// Releases the lock and executes queued actions.
play
.then(function() {
self._playLock = false;
if (!internal) {
self._emit('play', sound._id);
}
})
.catch(function() {
self._playLock = false;
self._emit('playerror', sound._id, 'Playback was unable to start. This is most commonly an issue ' +
'on mobile devices and Chrome where playback was not within a user interaction.');
});
} else if (!internal) {
self._emit('play', sound._id);
}
// Setting rate before playing won't work in IE, so we set it again here.
node.playbackRate = sound._rate;
// If the node is still paused, then we can assume there was a playback issue.
if (node.paused) {
self._emit('playerror', sound._id, 'Playback was unable to start. This is most commonly an issue ' +
'on mobile devices and Chrome where playback was not within a user interaction.');
return;
}
// Setup the end timer on sprites or listen for the ended event.
if (sprite !== '__default' || sound._loop) {
self._endTimers[sound._id] = setTimeout(self._ended.bind(self, sound), timeout);
} else {
self._endTimers[sound._id] = function() {
// Fire ended on this audio node.
self._ended(sound);
// Clear this listener.
node.removeEventListener('ended', self._endTimers[sound._id], false);
};
node.addEventListener('ended', self._endTimers[sound._id], false);
}
} catch (err) {
self._emit('playerror', sound._id, err);
}
};
// Play immediately if ready, or wait for the 'canplaythrough'e vent.
var loadedNoReadyState = (window && window.ejecta) || (!node.readyState && Howler._navigator.isCocoonJS);
if (node.readyState >= 3 || loadedNoReadyState) {
playHtml5();
} else {
var listener = function() {
// Begin playback.
playHtml5();
// Clear this listener.
node.removeEventListener(Howler._canPlayEvent, listener, false);
};
node.addEventListener(Howler._canPlayEvent, listener, false);
// Cancel the end timer.
self._clearTimer(sound._id);
}
}
return sound._id;
},
/**
* Pause playback and save current position.
* @param {Number} id The sound ID (empty to pause all in group).
* @return {Howl}
*/
pause: function(id) {
var self = this;
// If the sound hasn't loaded or a play() promise is pending, add it to the load queue to pause when capable.
if (self._state !== 'loaded' || self._playLock) {
self._queue.push({
event: 'pause',
action: function() {
self.pause(id);
}
});
return self;
}
// If no id is passed, get all ID's to be paused.
var ids = self._getSoundIds(id);
for (var i=0; i<ids.length; i++) {
// Clear the end timer.
self._clearTimer(ids[i]);
// Get the sound.
var sound = self._soundById(ids[i]);
if (sound && !sound._paused) {
// Reset the seek position.
sound._seek = self.seek(ids[i]);
sound._rateSeek = 0;
sound._paused = true;
// Stop currently running fades.
self._stopFade(ids[i]);
if (sound._node) {
if (self._webAudio) {
// Make sure the sound has been created.
if (!sound._node.bufferSource) {
continue;
}
if (typeof sound._node.bufferSource.stop === 'undefined') {
sound._node.bufferSource.noteOff(0);
} else {
sound._node.bufferSource.stop(0);
}
// Clean up the buffer source.
self._cleanBuffer(sound._node);
} else if (!isNaN(sound._node.duration) || sound._node.duration === Infinity) {
sound._node.pause();
}
}
}
// Fire the pause event, unless `true` is passed as the 2nd argument.
if (!arguments[1]) {
self._emit('pause', sound ? sound._id : null);
}
}
return self;
},
/**
* Stop playback and reset to start.
* @param {Number} id The sound ID (empty to stop all in group).
* @param {Boolean} internal Internal Use: true prevents event firing.
* @return {Howl}
*/
stop: function(id, internal) {
var self = this;
// If the sound hasn't loaded, add it to the load queue to stop when capable.
if (self._state !== 'loaded' || self._playLock) {
self._queue.push({
event: 'stop',
action: function() {
self.stop(id);
}
});
return self;
}
// If no id is passed, get all ID's to be stopped.
var ids = self._getSoundIds(id);
for (var i=0; i<ids.length; i++) {
// Clear the end timer.
self._clearTimer(ids[i]);
// Get the sound.
var sound = self._soundById(ids[i]);
if (sound) {
// Reset the seek position.
sound._seek = sound._start || 0;
sound._rateSeek = 0;
sound._paused = true;
sound._ended = true;
// Stop currently running fades.
self._stopFade(ids[i]);
if (sound._node) {
if (self._webAudio) {
// Make sure the sound's AudioBufferSourceNode has been created.
if (sound._node.bufferSource) {
if (typeof sound._node.bufferSource.stop === 'undefined') {
sound._node.bufferSource.noteOff(0);
} else {
sound._node.bufferSource.stop(0);
}
// Clean up the buffer source.
self._cleanBuffer(sound._node);
}
} else if (!isNaN(sound._node.duration) || sound._node.duration === Infinity) {
sound._node.currentTime = sound._start || 0;
sound._node.pause();
}
}
if (!internal) {
self._emit('stop', sound._id);
}
}
}
return self;
},
/**
* Mute/unmute a single sound or all sounds in this Howl group.
* @param {Boolean} muted Set to true to mute and false to unmute.
* @param {Number} id The sound ID to update (omit to mute/unmute all).
* @return {Howl}
*/
mute: function(muted, id) {
var self = this;
// If the sound hasn't loaded, add it to the load queue to mute when capable.
if (self._state !== 'loaded'|| self._playLock) {
self._queue.push({
event: 'mute',
action: function() {
self.mute(muted, id);
}
});
return self;
}
// If applying mute/unmute to all sounds, update the group's value.
if (typeof id === 'undefined') {
if (typeof muted === 'boolean') {
self._muted = muted;
} else {
return self._muted;
}
}
// If no id is passed, get all ID's to be muted.
var ids = self._getSoundIds(id);
for (var i=0; i<ids.length; i++) {
// Get the sound.
var sound = self._soundById(ids[i]);
if (sound) {
sound._muted = muted;
// Cancel active fade and set the volume to the end value.
if (sound._interval) {
self._stopFade(sound._id);
}
if (self._webAudio && sound._node) {
sound._node.gain.setValueAtTime(muted ? 0 : sound._volume, Howler.ctx.currentTime);
} else if (sound._node) {
sound._node.muted = Howler._muted ? true : muted;
}
self._emit('mute', sound._id);
}
}
return self;
},
/**
* Get/set the volume of this sound or of the Howl group. This method can optionally take 0, 1 or 2 arguments.
* volume() -> Returns the group's volume value.
* volume(id) -> Returns the sound id's current volume.
* volume(vol) -> Sets the volume of all sounds in this Howl group.
* volume(vol, id) -> Sets the volume of passed sound id.
* @return {Howl/Number} Returns self or current volume.
*/
volume: function() {
var self = this;
var args = arguments;
var vol, id;
// Determine the values based on arguments.
if (args.length === 0) {
// Return the value of the groups' volume.
return self._volume;
} else if (args.length === 1 || args.length === 2 && typeof args[1] === 'undefined') {
// First check if this is an ID, and if not, assume it is a new volume.
var ids = self._getSoundIds();
var index = ids.indexOf(args[0]);
if (index >= 0) {
id = parseInt(args[0], 10);
} else {
vol = parseFloat(args[0]);
}
} else if (args.length >= 2) {
vol = parseFloat(args[0]);
id = parseInt(args[1], 10);
}
// Update the volume or return the current volume.
var sound;
if (typeof vol !== 'undefined' && vol >= 0 && vol <= 1) {
// If the sound hasn't loaded, add it to the load queue to change volume when capable.
if (self._state !== 'loaded'|| self._playLock) {
self._queue.push({
event: 'volume',
action: function() {
self.volume.apply(self, args);
}
});
return self;
}
// Set the group volume.
if (typeof id === 'undefined') {
self._volume = vol;
}
// Update one or all volumes.
id = self._getSoundIds(id);
for (var i=0; i<id.length; i++) {
// Get the sound.
sound = self._soundById(id[i]);
if (sound) {
sound._volume = vol;
// Stop currently running fades.
if (!args[2]) {
self._stopFade(id[i]);
}
if (self._webAudio && sound._node && !sound._muted) {
sound._node.gain.setValueAtTime(vol, Howler.ctx.currentTime);
} else if (sound._node && !sound._muted) {
sound._node.volume = vol * Howler.volume();
}
self._emit('volume', sound._id);
}
}
} else {
sound = id ? self._soundById(id) : self._sounds[0];
return s