mirror of
https://github.com/scratchfoundation/scratch-audio.git
synced 2024-12-22 22:12:48 -05:00
Fall back to the older callback API for decodeAudioData
If promises are not supported, such as on Safari
This commit is contained in:
parent
bd1820ebf7
commit
aa2adf6c03
2 changed files with 26 additions and 2 deletions
|
@ -44,7 +44,20 @@ class DrumPlayer {
|
|||
request.responseType = 'arraybuffer';
|
||||
request.onload = () => {
|
||||
const audioData = request.response;
|
||||
this.audioContext.decodeAudioData(audioData).then(buffer => {
|
||||
// Check for newer promise-based API
|
||||
let loaderPromise;
|
||||
if (this.audioContext.decodeAudioData.length === 1) {
|
||||
loaderPromise = this.audioContext.decodeAudioData(audioData);
|
||||
} else {
|
||||
// Fall back to callback API
|
||||
loaderPromise = new Promise((resolve, reject) => {
|
||||
this.audioContext.decodeAudioData(audioData,
|
||||
decodedAudio => resolve(decodedAudio),
|
||||
error => reject(error)
|
||||
);
|
||||
});
|
||||
}
|
||||
loaderPromise.then(buffer => {
|
||||
this.drumSounds[i].setBuffer(buffer);
|
||||
});
|
||||
};
|
||||
|
|
11
src/index.js
11
src/index.js
|
@ -203,7 +203,18 @@ class AudioEngine {
|
|||
|
||||
switch (sound.format) {
|
||||
case '':
|
||||
// Check for newer promise-based API
|
||||
if (this.audioContext.decodeAudioData.length === 1) {
|
||||
loaderPromise = this.audioContext.decodeAudioData(bufferCopy);
|
||||
} else {
|
||||
// Fall back to callback API
|
||||
loaderPromise = new Promise((resolve, reject) => {
|
||||
this.audioContext.decodeAudioData(bufferCopy,
|
||||
decodedAudio => resolve(decodedAudio),
|
||||
error => reject(error)
|
||||
);
|
||||
});
|
||||
}
|
||||
break;
|
||||
case 'adpcm':
|
||||
loaderPromise = (new ADPCMSoundDecoder(this.audioContext)).decode(bufferCopy);
|
||||
|
|
Loading…
Reference in a new issue