Simplify audio loading, use AudioBuffer.loadFile(s)

This commit is contained in:
Joshua Granick
2016-10-05 15:30:21 -07:00
parent 944084d6ac
commit 31dacebe5e
3 changed files with 129 additions and 43 deletions

View File

@@ -153,7 +153,7 @@ class Preloader #if flash extends Sprite #end {
for (paths in soundPaths) { for (paths in soundPaths) {
var audioBuffer = AudioBuffer.fromFiles (paths); AudioBuffer.loadFiles (paths).onComplete (function (audioBuffer) {
for (path in paths) { for (path in paths) {
@@ -161,9 +161,9 @@ class Preloader #if flash extends Sprite #end {
} }
audioBuffer.src.on ("load", audioBuffer_onLoad); audioBuffer_onLoad ();
audioBuffer.src.on ("loaderror", audioBuffer_onLoad);
audioBuffer.src.load (); }).onError (audioBuffer_onLoad);
} }
@@ -301,7 +301,7 @@ class Preloader #if flash extends Sprite #end {
#if (js && html5) #if (js && html5)
private function audioBuffer_onLoad ():Void { private function audioBuffer_onLoad (?_):Void {
loaded++; loaded++;

View File

@@ -3,6 +3,8 @@ package lime.audio;
import haxe.io.Bytes; import haxe.io.Bytes;
import haxe.io.Path; import haxe.io.Path;
import lime.app.Future;
import lime.app.Promise;
import lime.audio.openal.AL; import lime.audio.openal.AL;
import lime.audio.openal.ALBuffer; import lime.audio.openal.ALBuffer;
import lime.utils.UInt8Array; import lime.utils.UInt8Array;
@@ -271,6 +273,116 @@ class AudioBuffer {
} }
public static function loadFile (path:String):lime.app.Future<AudioBuffer> {
var promise = new Promise<AudioBuffer> ();
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<String>):Future<AudioBuffer> {
var promise = new Promise<AudioBuffer> ();
#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<AudioBuffer> (function () return fromFiles (paths)));
#end
return promise.future;
}
// Get & Set Methods // Get & Set Methods

View File

@@ -472,45 +472,19 @@ class DefaultAssetLibrary extends AssetLibrary {
var promise = new Promise<AudioBuffer> (); var promise = new Promise<AudioBuffer> ();
#if flash if (Assets.isLocal (id)) {
if (path.exists (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)));
} else {
promise.complete (getAudioBuffer (id));
}
#else
promise.completeWith (new Future<AudioBuffer> (function () return getAudioBuffer (id))); promise.completeWith (new Future<AudioBuffer> (function () return getAudioBuffer (id)));
#end } else if (path.exists (id)) {
promise.completeWith (AudioBuffer.loadFile (path.get (id)));
} else {
promise.error (null);
}
return promise.future; return promise.future;