mirror of
https://we.phorge.it/source/phorge.git
synced 2024-12-18 19:40:55 +01:00
bfa5ffe8a1
Summary: Ref T5474. Allow columns to play a sound when tasks are dropped. This is a little tricky because Safari has changed somewhat recently to require some gymnastics to play sounds when the user didn't explicitly click something. Preloading the sound on the first mouse interaction, then playing and immediately pausing it seems to work, though. Test Plan: Added a trigger with 5 sounds. In Safari, Chrome, and Firefox, dropped a card into the column. In all browsers, heard a nice sequence of 5 sounds played one after the other. Reviewers: amckinley Reviewed By: amckinley Maniphest Tasks: T5474 Differential Revision: https://secure.phabricator.com/D20306
82 lines
1.8 KiB
JavaScript
82 lines
1.8 KiB
JavaScript
/**
|
|
* @requires javelin-install
|
|
* @provides javelin-sound
|
|
* @javelin
|
|
*/
|
|
|
|
|
|
JX.install('Sound', {
|
|
statics: {
|
|
_sounds: {},
|
|
_queue: [],
|
|
_playingQueue: false,
|
|
|
|
load: function(uri) {
|
|
var self = JX.Sound;
|
|
|
|
if (!(uri in self._sounds)) {
|
|
var audio = JX.$N(
|
|
'audio',
|
|
{
|
|
src: uri,
|
|
preload: 'auto'
|
|
});
|
|
|
|
// 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;
|
|
}
|
|
},
|
|
|
|
play: function(uri, callback) {
|
|
var self = JX.Sound;
|
|
self.load(uri);
|
|
|
|
var sound = self._sounds[uri];
|
|
|
|
try {
|
|
sound.onended = callback || JX.bag;
|
|
sound.play().then(JX.bag, callback || JX.bag);
|
|
} catch (ex) {
|
|
JX.log(ex);
|
|
}
|
|
},
|
|
|
|
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;
|
|
}
|
|
}
|
|
|
|
}
|
|
});
|