mirror of
https://github.com/scratchfoundation/scratch-audio.git
synced 2025-01-09 22:32:33 -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.responseType = 'arraybuffer';
|
||||||
request.onload = () => {
|
request.onload = () => {
|
||||||
const audioData = request.response;
|
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);
|
this.drumSounds[i].setBuffer(buffer);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
11
src/index.js
11
src/index.js
|
@ -204,7 +204,18 @@ class AudioEngine {
|
||||||
|
|
||||||
switch (sound.format) {
|
switch (sound.format) {
|
||||||
case '':
|
case '':
|
||||||
|
// Check for newer promise-based API
|
||||||
|
if (this.audioContext.decodeAudioData.length === 1) {
|
||||||
loaderPromise = this.audioContext.decodeAudioData(bufferCopy);
|
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;
|
break;
|
||||||
case 'adpcm':
|
case 'adpcm':
|
||||||
loaderPromise = (new ADPCMSoundDecoder(this.audioContext)).decode(bufferCopy);
|
loaderPromise = (new ADPCMSoundDecoder(this.audioContext)).decode(bufferCopy);
|
||||||
|
|
Loading…
Reference in a new issue