From 4778ab8d4f00c2dc439c5c7980261bd237e247ec Mon Sep 17 00:00:00 2001 From: Eric Rosenbaum Date: Mon, 22 Jan 2018 10:15:41 -0500 Subject: [PATCH] Fix music extension asset loading on safari --- src/extensions/scratch3_music/index.js | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/extensions/scratch3_music/index.js b/src/extensions/scratch3_music/index.js index 0e14a2150..bbec66a47 100644 --- a/src/extensions/scratch3_music/index.js +++ b/src/extensions/scratch3_music/index.js @@ -97,10 +97,23 @@ class Scratch3MusicBlocks { _loadSound (fileName, index, bufferArray) { if (!this.runtime.storage) return; if (!this.runtime.audioEngine) return; + if (!this.runtime.audioEngine.audioContext) return; return this.runtime.storage.load(this.runtime.storage.AssetType.Sound, fileName, 'mp3') - .then(soundAsset => - this.runtime.audioEngine.audioContext.decodeAudioData(soundAsset.data.buffer) - ) + .then(soundAsset => { + const context = this.runtime.audioEngine.audioContext; + // Check for newer promise-based API + if (context.decodeAudioData.length === 1) { + return context.decodeAudioData(soundAsset.data.buffer); + } else { // eslint-disable no-else-return + // Fall back to callback API + return new Promise((resolve, reject) => + context.decodeAudioData(soundAsset.data.buffer, + buffer => resolve(buffer), + error => reject(error) + ) + ); + } + }) .then(buffer => { bufferArray[index] = buffer; });