fix safari audio bug where error prevents leaving worldmap locations -- reveals another safari bug where decodeAudioData doesn't return a promise to start playing music immediately

This commit is contained in:
jorsi
2020-10-09 12:22:06 -04:00
parent d0a12ac4a5
commit 9d8049988d
2 changed files with 29 additions and 20 deletions
+20 -2
View File
@@ -136,7 +136,9 @@ var AudioEngine = {
var fadeTime = AudioEngine._audioContext.currentTime + AudioEngine.FADE_TIME * 2; var fadeTime = AudioEngine._audioContext.currentTime + AudioEngine.FADE_TIME * 2;
// fade out event music and stop // fade out event music and stop
if (AudioEngine._currentEventAudio) { if (AudioEngine._currentEventAudio &&
AudioEngine._currentEventAudio.source &&
AudioEngine._currentEventAudio.source.buffer) {
var currentEventGainValue = AudioEngine._currentEventAudio.envelope.gain.value; var currentEventGainValue = AudioEngine._currentEventAudio.envelope.gain.value;
AudioEngine._currentEventAudio.envelope.gain.cancelScheduledValues(AudioEngine._audioContext.currentTime); AudioEngine._currentEventAudio.envelope.gain.cancelScheduledValues(AudioEngine._audioContext.currentTime);
AudioEngine._currentEventAudio.envelope.gain.setValueAtTime(currentEventGainValue, AudioEngine._audioContext.currentTime); AudioEngine._currentEventAudio.envelope.gain.setValueAtTime(currentEventGainValue, AudioEngine._audioContext.currentTime);
@@ -212,10 +214,26 @@ var AudioEngine = {
return AudioEngine._getMissingAudioBuffer(); return AudioEngine._getMissingAudioBuffer();
} }
return AudioEngine._audioContext.decodeAudioData(buffer, function (decodedData) { var decodeAudioDataPromise = AudioEngine._audioContext.decodeAudioData(buffer, function (decodedData) {
AudioEngine.AUDIO_BUFFER_CACHE[src] = decodedData; AudioEngine.AUDIO_BUFFER_CACHE[src] = decodedData;
return AudioEngine.AUDIO_BUFFER_CACHE[src]; return AudioEngine.AUDIO_BUFFER_CACHE[src];
}); });
// Safari WebAudio does not return a promise based API for
// decodeAudioData, so we need to fake it if we want to play
// audio immediately on first fetch
if (decodeAudioDataPromise) {
return decodeAudioDataPromise;
} else {
return new Promise(function (resolve, reject) {
var fakePromiseId = setInterval(function() {
if (AudioEngine.AUDIO_BUFFER_CACHE[src]) {
resolve(AudioEngine.AUDIO_BUFFER_CACHE[src]);
clearInterval(fakePromiseId);
}
}, 20);
});
}
}); });
} }
}, },
+9 -18
View File
@@ -142,17 +142,10 @@
}); });
} }
// Disable this for now
// $('<span>')
// .addClass('volume menuBtn')
// .text(_('sound on.'))
// .click(() => Engine.toggleVolume())
// .appendTo(menu);
$('<span>') $('<span>')
.addClass('volume menuBtn') .addClass('volume menuBtn')
.text(_('sound off.')) .text(_('sound on.'))
.click(Engine.toggleVolume) .click(() => Engine.toggleVolume())
.appendTo(menu); .appendTo(menu);
$('<span>') $('<span>')
@@ -222,7 +215,7 @@
$.Dispatch('stateUpdate').subscribe(Engine.handleStateUpdates); $.Dispatch('stateUpdate').subscribe(Engine.handleStateUpdates);
$SM.init(); $SM.init();
// AudioEngine.init(); Disable this for now AudioEngine.init();
Notifications.init(); Notifications.init();
Events.init(); Events.init();
Room.init(); Room.init();
@@ -246,24 +239,22 @@
Engine.triggerHyperMode(); Engine.triggerHyperMode();
} }
// Disable this for now Engine.toggleVolume(Boolean($SM.get('config.soundOn')));
// Engine.toggleVolume(Boolean($SM.get('config.soundOn'))); if(!AudioEngine.isAudioContextRunning()){
// if(!AudioEngine.isAudioContextRunning()){ document.addEventListener('click', Engine.resumeAudioContext, true);
// document.addEventListener('click', Engine.resumeAudioContext, true); }
// }
Engine.saveLanguage(); Engine.saveLanguage();
Engine.travelTo(Room); Engine.travelTo(Room);
// Disable this for now setTimeout(notifyAboutSound, 3000);
// setTimeout(notifyAboutSound, 3000);
}, },
resumeAudioContext: function () { resumeAudioContext: function () {
AudioEngine.tryResumingAudioContext(); AudioEngine.tryResumingAudioContext();
// turn on music! // turn on music!
AudioEngine.setMasterVolume($SM.get('config.soundOn') ? 1.0 : 0.0, 0); AudioEngine.setMasterVolume($SM.get('config.soundOn') ? 1.0 : 0.0, 0);
document.removeEventListener('click', Engine.resumeAudioContext); document.removeEventListener('click', Engine.resumeAudioContext);
}, },