From 31dacebe5e1f5d56c4ae49274b01431a9d7b841f Mon Sep 17 00:00:00 2001 From: Joshua Granick Date: Wed, 5 Oct 2016 15:30:21 -0700 Subject: [PATCH] Simplify audio loading, use AudioBuffer.loadFile(s) --- lime/app/Preloader.hx | 20 ++--- lime/audio/AudioBuffer.hx | 112 ++++++++++++++++++++++++++ templates/haxe/DefaultAssetLibrary.hx | 40 ++------- 3 files changed, 129 insertions(+), 43 deletions(-) diff --git a/lime/app/Preloader.hx b/lime/app/Preloader.hx index 7a11a4525..f1bedd6af 100644 --- a/lime/app/Preloader.hx +++ b/lime/app/Preloader.hx @@ -153,17 +153,17 @@ class Preloader #if flash extends Sprite #end { for (paths in soundPaths) { - var audioBuffer = AudioBuffer.fromFiles (paths); - - for (path in paths) { + AudioBuffer.loadFiles (paths).onComplete (function (audioBuffer) { - audioBuffers.set (path, audioBuffer); + for (path in paths) { + + audioBuffers.set (path, audioBuffer); + + } - } - - audioBuffer.src.on ("load", audioBuffer_onLoad); - audioBuffer.src.on ("loaderror", audioBuffer_onLoad); - audioBuffer.src.load (); + audioBuffer_onLoad (); + + }).onError (audioBuffer_onLoad); } @@ -301,7 +301,7 @@ class Preloader #if flash extends Sprite #end { #if (js && html5) - private function audioBuffer_onLoad ():Void { + private function audioBuffer_onLoad (?_):Void { loaded++; diff --git a/lime/audio/AudioBuffer.hx b/lime/audio/AudioBuffer.hx index 92bb1d427..dd04fdb2b 100644 --- a/lime/audio/AudioBuffer.hx +++ b/lime/audio/AudioBuffer.hx @@ -3,6 +3,8 @@ package lime.audio; import haxe.io.Bytes; import haxe.io.Path; +import lime.app.Future; +import lime.app.Promise; import lime.audio.openal.AL; import lime.audio.openal.ALBuffer; import lime.utils.UInt8Array; @@ -271,6 +273,116 @@ class AudioBuffer { } + public static function loadFile (path:String):lime.app.Future { + + var promise = new Promise (); + + var audioBuffer = AudioBuffer.fromFile (path); + + if (audioBuffer != null) { + + #if flash + + audioBuffer.__srcSound.addEventListener (flash.events.Event.COMPLETE, function (event) { + + promise.complete (audioBuffer); + + }); + + audioBuffer.__srcSound.addEventListener (flash.events.ProgressEvent.PROGRESS, function (event) { + + if (event.bytesTotal == 0) { + + promise.progress (0); + + } else { + + promise.progress (event.bytesLoaded / event.bytesTotal); + + } + + }); + + audioBuffer.__srcSound.addEventListener (IOErrorEvent.IO_ERROR, promise.error); + + #elseif (js && html5 && howlerjs) + + if (audioBuffer != null) { + + audioBuffer.__srcHowl.on ("load", function () { + + promise.complete (audioBuffer); + + }); + + audioBuffer.__srcHowl.on ("loaderror", function () { + + promise.error (null); + + }); + + audioBuffer.__srcHowl.load (); + + } + + #else + + promise.complete (audioBuffer); + + #end + + } else { + + promise.error (null); + + } + + return promise.future; + + } + + + public static function loadFiles (paths:Array):Future { + + var promise = new Promise (); + + #if (js && html5 && howlerjs) + + var audioBuffer = AudioBuffer.fromFiles (paths); + + if (audioBuffer != null) { + + audioBuffer.__srcHowl.on ("load", function () { + + promise.complete (audioBuffer); + + }); + + audioBuffer.__srcHowl.on ("loaderror", function () { + + promise.error (null); + + }); + + audioBuffer.__srcHowl.load (); + + } else { + + promise.error (null); + + } + + #else + + promise.completeWith (new Future (function () return fromFiles (paths))); + + #end + + return promise.future; + + } + + // Get & Set Methods diff --git a/templates/haxe/DefaultAssetLibrary.hx b/templates/haxe/DefaultAssetLibrary.hx index ab2a32557..dbfd491fa 100644 --- a/templates/haxe/DefaultAssetLibrary.hx +++ b/templates/haxe/DefaultAssetLibrary.hx @@ -472,46 +472,20 @@ class DefaultAssetLibrary extends AssetLibrary { var promise = new Promise (); - #if flash - - if (path.exists (id)) { + if (Assets.isLocal (id)) { - var soundLoader = new Sound (); - soundLoader.addEventListener (Event.COMPLETE, function (event) { - - var audioBuffer:AudioBuffer = new AudioBuffer(); - audioBuffer.src = event.currentTarget; - promise.complete (audioBuffer); - - }); - soundLoader.addEventListener (ProgressEvent.PROGRESS, function (event) { - - if (event.bytesTotal == 0) { - - promise.progress (0); - - } else { - - promise.progress (event.bytesLoaded / event.bytesTotal); - - } - - }); - soundLoader.addEventListener (IOErrorEvent.IO_ERROR, promise.error); - soundLoader.load (new URLRequest (path.get (id))); + promise.completeWith (new Future (function () return getAudioBuffer (id))); + + } else if (path.exists (id)) { + + promise.completeWith (AudioBuffer.loadFile (path.get (id))); } else { - promise.complete (getAudioBuffer (id)); + promise.error (null); } - #else - - promise.completeWith (new Future (function () return getAudioBuffer (id))); - - #end - return promise.future; }