Fall back to the older callback API for decodeAudioData

If promises are not supported, such as on Safari
This commit is contained in:
Eric Rosenbaum 2017-07-21 12:35:21 -04:00
parent bd1820ebf7
commit aa2adf6c03
2 changed files with 26 additions and 2 deletions

View file

@ -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);
});
};

View file

@ -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);