2015-03-10 22:20:00 +01:00
|
|
|
/**
|
|
|
|
* @requires javelin-install
|
|
|
|
* @provides javelin-sound
|
|
|
|
* @javelin
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
JX.install('Sound', {
|
|
|
|
statics: {
|
|
|
|
_sounds: {},
|
2019-03-21 23:22:09 +01:00
|
|
|
_queue: [],
|
|
|
|
_playingQueue: false,
|
2015-03-10 22:20:00 +01:00
|
|
|
|
|
|
|
load: function(uri) {
|
|
|
|
var self = JX.Sound;
|
|
|
|
|
|
|
|
if (!(uri in self._sounds)) {
|
2019-03-21 23:22:09 +01:00
|
|
|
var audio = JX.$N(
|
2015-03-10 22:20:00 +01:00
|
|
|
'audio',
|
|
|
|
{
|
|
|
|
src: uri,
|
|
|
|
preload: 'auto'
|
|
|
|
});
|
2019-03-21 23:22:09 +01:00
|
|
|
|
|
|
|
// In Safari, it isn't good enough to just load a sound in response
|
|
|
|
// to a click: we must also play it. Once we've played it once, we
|
|
|
|
// can continue to play it freely.
|
|
|
|
|
|
|
|
// Play the sound, then immediately pause it. This rejects the "play()"
|
|
|
|
// promise but marks the audio as playable, so our "play()" method will
|
|
|
|
// work correctly later.
|
|
|
|
if (window.webkitAudioContext) {
|
|
|
|
audio.play().then(JX.bag, JX.bag);
|
|
|
|
audio.pause();
|
|
|
|
}
|
|
|
|
|
|
|
|
self._sounds[uri] = audio;
|
2015-03-10 22:20:00 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-03-21 23:22:09 +01:00
|
|
|
play: function(uri, callback) {
|
2015-03-10 22:20:00 +01:00
|
|
|
var self = JX.Sound;
|
|
|
|
self.load(uri);
|
|
|
|
|
|
|
|
var sound = self._sounds[uri];
|
|
|
|
|
|
|
|
try {
|
2019-03-21 23:22:09 +01:00
|
|
|
sound.onended = callback || JX.bag;
|
|
|
|
sound.play().then(JX.bag, callback || JX.bag);
|
2015-03-10 22:20:00 +01:00
|
|
|
} catch (ex) {
|
|
|
|
JX.log(ex);
|
|
|
|
}
|
2019-03-21 23:22:09 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
queue: function(uri) {
|
|
|
|
var self = JX.Sound;
|
|
|
|
self._queue.push(uri);
|
|
|
|
self._playQueue();
|
|
|
|
},
|
|
|
|
|
|
|
|
_playQueue: function() {
|
|
|
|
var self = JX.Sound;
|
|
|
|
if (self._playingQueue) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
self._playingQueue = true;
|
|
|
|
self._nextQueue();
|
|
|
|
},
|
|
|
|
|
|
|
|
_nextQueue: function() {
|
|
|
|
var self = JX.Sound;
|
|
|
|
if (self._queue.length) {
|
|
|
|
var next = self._queue[0];
|
|
|
|
self._queue.splice(0, 1);
|
|
|
|
self.play(next, self._nextQueue);
|
|
|
|
} else {
|
|
|
|
self._playingQueue = false;
|
|
|
|
}
|
2015-03-10 22:20:00 +01:00
|
|
|
}
|
2019-03-21 23:22:09 +01:00
|
|
|
|
2015-03-10 22:20:00 +01:00
|
|
|
}
|
|
|
|
});
|