From 6d9f889aa79500b6d1abd1ad3ec32bc34b61539c Mon Sep 17 00:00:00 2001 From: Eric Rosenbaum Date: Mon, 17 Oct 2016 17:16:55 -0400 Subject: [PATCH] fixed sound loading --- src/blocks/scratch3_sound.js | 40 +++++++++++++++++------------------- src/sprites/clone.js | 5 ++++- 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/src/blocks/scratch3_sound.js b/src/blocks/scratch3_sound.js index 53881b9dd..5964270a5 100644 --- a/src/blocks/scratch3_sound.js +++ b/src/blocks/scratch3_sound.js @@ -1,4 +1,3 @@ -// var Cast = require('../util/cast'); var MathUtil = require('../util/math-util'); var Promise = require('promise'); @@ -31,13 +30,26 @@ Scratch3SoundBlocks.prototype.getPrimitives = function() { }; Scratch3SoundBlocks.prototype.playSound = function (args, util) { - var url = this._getSoundUrl(args.SOUND_MENU, util); - util.target.audioEngine.playSoundFromUrl(url); + var index = this._getSoundIndex(args.SOUND_MENU, util); + util.target.audioEngine.playSound(index); }; -Scratch3SoundBlocks.prototype._getSoundUrl = function (soundName, util) { +Scratch3SoundBlocks.prototype.playSoundAndWait = function (args, util) { + var index = this._getSoundIndex(args.SOUND_MENU, util); + util.target.audioEngine.playSound(index); + + var duration = util.target.audioEngine.getSoundDuration(index); + + return new Promise(function(resolve) { + setTimeout(function() { + resolve(); + }, 1000*duration); + }); +}; + +Scratch3SoundBlocks.prototype._getSoundIndex = function (soundName, util) { if (util.target.sprite.sounds.length == 0) { - return ''; + return 0; } var index; if (typeof soundName === 'number') { @@ -46,24 +58,10 @@ Scratch3SoundBlocks.prototype._getSoundUrl = function (soundName, util) { } else { index = util.target.getSoundIndexByName(soundName); if (index == -1) { - return ''; + index = 0; } } - return util.target.sprite.sounds[index].fileUrl; -}; - - -Scratch3SoundBlocks.prototype.playSoundAndWait = function (args, util) { - var url = this._getSoundUrl(args.SOUND_MENU, util); - util.target.audioEngine.playSoundFromUrl(url); - - var duration = util.target.audioEngine.getSoundDuration(url); - - return new Promise(function(resolve) { - setTimeout(function() { - resolve(); - }, 1000*duration); - }); + return index; }; Scratch3SoundBlocks.prototype.stopAllSounds = function (args, util) { diff --git a/src/sprites/clone.js b/src/sprites/clone.js index 87d65d706..b96440201 100644 --- a/src/sprites/clone.js +++ b/src/sprites/clone.js @@ -48,7 +48,10 @@ function Clone(sprite, runtime) { /** * Audio engine */ - this.audioEngine = new AudioEngine(); + this.audioEngine = null; + if (this.runtime) { + this.audioEngine = new AudioEngine(this.sprite.sounds); + } } util.inherits(Clone, Target);