Use soundId instead of md5 for playing sounds

This commit is contained in:
Paul Kaplan 2017-07-25 12:17:20 -04:00
parent 912e7daa81
commit 44298fd71b
3 changed files with 22 additions and 19 deletions

View file

@ -113,18 +113,18 @@ class Scratch3SoundBlocks {
playSound (args, util) { playSound (args, util) {
const index = this._getSoundIndex(args.SOUND_MENU, util); const index = this._getSoundIndex(args.SOUND_MENU, util);
if (index >= 0) { if (index >= 0) {
const md5 = util.target.sprite.sounds[index].md5; const soundId = util.target.sprite.sounds[index].soundId;
if (util.target.audioPlayer === null) return; if (util.target.audioPlayer === null) return;
util.target.audioPlayer.playSound(md5); util.target.audioPlayer.playSound(soundId);
} }
} }
playSoundAndWait (args, util) { playSoundAndWait (args, util) {
const index = this._getSoundIndex(args.SOUND_MENU, util); const index = this._getSoundIndex(args.SOUND_MENU, util);
if (index >= 0) { if (index >= 0) {
const md5 = util.target.sprite.sounds[index].md5; const soundId = util.target.sprite.sounds[index].soundId;
if (util.target.audioPlayer === null) return; if (util.target.audioPlayer === null) return;
return util.target.audioPlayer.playSound(md5); return util.target.audioPlayer.playSound(soundId);
} }
} }

View file

@ -31,7 +31,10 @@ const loadSound = function (sound, runtime) {
{data: soundAsset.data} {data: soundAsset.data}
)); ));
}) })
.then(() => sound); .then(soundId => {
sound.soundId = soundId;
return sound;
});
}; };
module.exports = loadSound; module.exports = loadSound;

View file

@ -17,14 +17,14 @@ const util = {
target: { target: {
sprite: { sprite: {
sounds: [ sounds: [
{name: 'first name', md5: 'first md5'}, {name: 'first name', soundId: 'first soundId'},
{name: 'second name', md5: 'second md5'}, {name: 'second name', soundId: 'second soundId'},
{name: 'third name', md5: 'third md5'}, {name: 'third name', soundId: 'third soundId'},
{name: '6', md5: 'fourth md5'} {name: '6', soundId: 'fourth soundId'}
] ]
}, },
audioPlayer: { audioPlayer: {
playSound: md5 => (playedSound = md5), playSound: soundId => (playedSound = soundId),
playDrumForBeats: drum => (playedDrum = drum) playDrumForBeats: drum => (playedDrum = drum)
} }
} }
@ -33,37 +33,37 @@ const util = {
test('playSound with a name string works', t => { test('playSound with a name string works', t => {
const args = {SOUND_MENU: 'second name'}; const args = {SOUND_MENU: 'second name'};
blocks.playSound(args, util); blocks.playSound(args, util);
t.strictEqual(playedSound, 'second md5'); t.strictEqual(playedSound, 'second soundId');
t.end(); t.end();
}); });
test('playSound with a number string works 1-indexed', t => { test('playSound with a number string works 1-indexed', t => {
let args = {SOUND_MENU: '5'}; let args = {SOUND_MENU: '5'};
blocks.playSound(args, util); blocks.playSound(args, util);
t.strictEqual(playedSound, 'first md5'); t.strictEqual(playedSound, 'first soundId');
args = {SOUND_MENU: '1'}; args = {SOUND_MENU: '1'};
blocks.playSound(args, util); blocks.playSound(args, util);
t.strictEqual(playedSound, 'first md5'); t.strictEqual(playedSound, 'first soundId');
args = {SOUND_MENU: '0'}; args = {SOUND_MENU: '0'};
blocks.playSound(args, util); blocks.playSound(args, util);
t.strictEqual(playedSound, 'fourth md5'); t.strictEqual(playedSound, 'fourth soundId');
t.end(); t.end();
}); });
test('playSound with a number works 1-indexed', t => { test('playSound with a number works 1-indexed', t => {
let args = {SOUND_MENU: 5}; let args = {SOUND_MENU: 5};
blocks.playSound(args, util); blocks.playSound(args, util);
t.strictEqual(playedSound, 'first md5'); t.strictEqual(playedSound, 'first soundId');
args = {SOUND_MENU: 1}; args = {SOUND_MENU: 1};
blocks.playSound(args, util); blocks.playSound(args, util);
t.strictEqual(playedSound, 'first md5'); t.strictEqual(playedSound, 'first soundId');
args = {SOUND_MENU: 0}; args = {SOUND_MENU: 0};
blocks.playSound(args, util); blocks.playSound(args, util);
t.strictEqual(playedSound, 'fourth md5'); t.strictEqual(playedSound, 'fourth soundId');
t.end(); t.end();
}); });
@ -71,7 +71,7 @@ test('playSound prioritizes sound index if given a number', t => {
const args = {SOUND_MENU: 6}; const args = {SOUND_MENU: 6};
blocks.playSound(args, util); blocks.playSound(args, util);
// Ignore the sound named '6', wrapClamp to the second instead // Ignore the sound named '6', wrapClamp to the second instead
t.strictEqual(playedSound, 'second md5'); t.strictEqual(playedSound, 'second soundId');
t.end(); t.end();
}); });
@ -79,7 +79,7 @@ test('playSound prioritizes sound name if given a string', t => {
const args = {SOUND_MENU: '6'}; const args = {SOUND_MENU: '6'};
blocks.playSound(args, util); blocks.playSound(args, util);
// Use the sound named '6', which is the fourth // Use the sound named '6', which is the fourth
t.strictEqual(playedSound, 'fourth md5'); t.strictEqual(playedSound, 'fourth soundId');
t.end(); t.end();
}); });