Merge pull request #56 from LLK/feature/decodeaudiodata-safari

Fall back to the older callback API for decodeAudioData
This commit is contained in:
Eric Rosenbaum 2017-07-25 11:35:09 -04:00 committed by GitHub
commit f26dbde03b
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

@ -204,7 +204,18 @@ class AudioEngine {
switch (sound.format) {
case '':
loaderPromise = this.audioContext.decodeAudioData(bufferCopy);
// 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);