Simplify audio loading, use AudioBuffer.loadFile(s)
This commit is contained in:
@@ -153,7 +153,7 @@ class Preloader #if flash extends Sprite #end {
|
||||
|
||||
for (paths in soundPaths) {
|
||||
|
||||
var audioBuffer = AudioBuffer.fromFiles (paths);
|
||||
AudioBuffer.loadFiles (paths).onComplete (function (audioBuffer) {
|
||||
|
||||
for (path in paths) {
|
||||
|
||||
@@ -161,9 +161,9 @@ class Preloader #if flash extends Sprite #end {
|
||||
|
||||
}
|
||||
|
||||
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++;
|
||||
|
||||
|
||||
@@ -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<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
|
||||
|
||||
@@ -472,45 +472,19 @@ class DefaultAssetLibrary extends AssetLibrary {
|
||||
|
||||
var promise = new Promise<AudioBuffer> ();
|
||||
|
||||
#if flash
|
||||
|
||||
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
|
||||
if (Assets.isLocal (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;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user