mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-01-10 15:02:06 -05:00
Use soundId instead of md5 for playing sounds
This commit is contained in:
parent
912e7daa81
commit
44298fd71b
3 changed files with 22 additions and 19 deletions
|
@ -113,18 +113,18 @@ class Scratch3SoundBlocks {
|
|||
playSound (args, util) {
|
||||
const index = this._getSoundIndex(args.SOUND_MENU, util);
|
||||
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;
|
||||
util.target.audioPlayer.playSound(md5);
|
||||
util.target.audioPlayer.playSound(soundId);
|
||||
}
|
||||
}
|
||||
|
||||
playSoundAndWait (args, util) {
|
||||
const index = this._getSoundIndex(args.SOUND_MENU, util);
|
||||
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;
|
||||
return util.target.audioPlayer.playSound(md5);
|
||||
return util.target.audioPlayer.playSound(soundId);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -31,7 +31,10 @@ const loadSound = function (sound, runtime) {
|
|||
{data: soundAsset.data}
|
||||
));
|
||||
})
|
||||
.then(() => sound);
|
||||
.then(soundId => {
|
||||
sound.soundId = soundId;
|
||||
return sound;
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = loadSound;
|
||||
|
|
|
@ -17,14 +17,14 @@ const util = {
|
|||
target: {
|
||||
sprite: {
|
||||
sounds: [
|
||||
{name: 'first name', md5: 'first md5'},
|
||||
{name: 'second name', md5: 'second md5'},
|
||||
{name: 'third name', md5: 'third md5'},
|
||||
{name: '6', md5: 'fourth md5'}
|
||||
{name: 'first name', soundId: 'first soundId'},
|
||||
{name: 'second name', soundId: 'second soundId'},
|
||||
{name: 'third name', soundId: 'third soundId'},
|
||||
{name: '6', soundId: 'fourth soundId'}
|
||||
]
|
||||
},
|
||||
audioPlayer: {
|
||||
playSound: md5 => (playedSound = md5),
|
||||
playSound: soundId => (playedSound = soundId),
|
||||
playDrumForBeats: drum => (playedDrum = drum)
|
||||
}
|
||||
}
|
||||
|
@ -33,37 +33,37 @@ const util = {
|
|||
test('playSound with a name string works', t => {
|
||||
const args = {SOUND_MENU: 'second name'};
|
||||
blocks.playSound(args, util);
|
||||
t.strictEqual(playedSound, 'second md5');
|
||||
t.strictEqual(playedSound, 'second soundId');
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('playSound with a number string works 1-indexed', t => {
|
||||
let args = {SOUND_MENU: '5'};
|
||||
blocks.playSound(args, util);
|
||||
t.strictEqual(playedSound, 'first md5');
|
||||
t.strictEqual(playedSound, 'first soundId');
|
||||
|
||||
args = {SOUND_MENU: '1'};
|
||||
blocks.playSound(args, util);
|
||||
t.strictEqual(playedSound, 'first md5');
|
||||
t.strictEqual(playedSound, 'first soundId');
|
||||
|
||||
args = {SOUND_MENU: '0'};
|
||||
blocks.playSound(args, util);
|
||||
t.strictEqual(playedSound, 'fourth md5');
|
||||
t.strictEqual(playedSound, 'fourth soundId');
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('playSound with a number works 1-indexed', t => {
|
||||
let args = {SOUND_MENU: 5};
|
||||
blocks.playSound(args, util);
|
||||
t.strictEqual(playedSound, 'first md5');
|
||||
t.strictEqual(playedSound, 'first soundId');
|
||||
|
||||
args = {SOUND_MENU: 1};
|
||||
blocks.playSound(args, util);
|
||||
t.strictEqual(playedSound, 'first md5');
|
||||
t.strictEqual(playedSound, 'first soundId');
|
||||
|
||||
args = {SOUND_MENU: 0};
|
||||
blocks.playSound(args, util);
|
||||
t.strictEqual(playedSound, 'fourth md5');
|
||||
t.strictEqual(playedSound, 'fourth soundId');
|
||||
t.end();
|
||||
});
|
||||
|
||||
|
@ -71,7 +71,7 @@ test('playSound prioritizes sound index if given a number', t => {
|
|||
const args = {SOUND_MENU: 6};
|
||||
blocks.playSound(args, util);
|
||||
// Ignore the sound named '6', wrapClamp to the second instead
|
||||
t.strictEqual(playedSound, 'second md5');
|
||||
t.strictEqual(playedSound, 'second soundId');
|
||||
t.end();
|
||||
});
|
||||
|
||||
|
@ -79,7 +79,7 @@ test('playSound prioritizes sound name if given a string', t => {
|
|||
const args = {SOUND_MENU: '6'};
|
||||
blocks.playSound(args, util);
|
||||
// Use the sound named '6', which is the fourth
|
||||
t.strictEqual(playedSound, 'fourth md5');
|
||||
t.strictEqual(playedSound, 'fourth soundId');
|
||||
t.end();
|
||||
});
|
||||
|
||||
|
|
Loading…
Reference in a new issue