mirror of
https://github.com/scratchfoundation/scratch-audio.git
synced 2024-12-22 14:02:29 -05:00
Merge pull request #56 from LLK/feature/decodeaudiodata-safari
Fall back to the older callback API for decodeAudioData
This commit is contained in:
commit
f26dbde03b
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);
|
||||
});
|
||||
};
|
||||
|
|
13
src/index.js
13
src/index.js
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue