forked from LeenkxTeam/LNXSDK
Update Files
This commit is contained in:
132
Kha/Backends/HTML5/kha/js/Sound.hx
Normal file
132
Kha/Backends/HTML5/kha/js/Sound.hx
Normal file
@ -0,0 +1,132 @@
|
||||
package kha.js;
|
||||
|
||||
import js.Browser;
|
||||
import js.html.AudioElement;
|
||||
import js.html.ErrorEvent;
|
||||
import js.html.Event;
|
||||
import js.html.MediaError;
|
||||
|
||||
using StringTools;
|
||||
|
||||
/*class SoundChannel extends kha.SoundChannel {
|
||||
private var element: Dynamic;
|
||||
|
||||
public function new(element: Dynamic) {
|
||||
super();
|
||||
this.element = element;
|
||||
}
|
||||
|
||||
override public function play(): Void {
|
||||
super.play();
|
||||
element.play();
|
||||
}
|
||||
|
||||
override public function pause(): Void {
|
||||
try {
|
||||
element.pause();
|
||||
}
|
||||
catch (e: Dynamic) {
|
||||
trace(e);
|
||||
}
|
||||
}
|
||||
|
||||
override public function stop(): Void {
|
||||
try {
|
||||
element.pause();
|
||||
element.currentTime = 0;
|
||||
super.stop();
|
||||
}
|
||||
catch (e: Dynamic) {
|
||||
trace(e);
|
||||
}
|
||||
}
|
||||
|
||||
override public function getCurrentPos(): Int {
|
||||
return Math.ceil(element.currentTime * 1000); // Miliseconds
|
||||
}
|
||||
|
||||
override public function getLength(): Int {
|
||||
if (Math.isFinite(element.duration)) {
|
||||
return Math.floor(element.duration * 1000); // Miliseconds
|
||||
}
|
||||
else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}*/
|
||||
class Sound extends kha.Sound {
|
||||
var filenames: Array<String>;
|
||||
|
||||
static var loading: Array<Sound> = new Array();
|
||||
|
||||
var done: kha.Sound->Void;
|
||||
var failed: AssetError->Void;
|
||||
|
||||
public var element: AudioElement;
|
||||
|
||||
public function new(filenames: Array<String>, done: kha.Sound->Void, failed: AssetError->Void) {
|
||||
super();
|
||||
|
||||
this.done = done;
|
||||
this.failed = failed;
|
||||
loading.push(this); // prevent gc from removing this
|
||||
|
||||
element = Browser.document.createAudioElement();
|
||||
|
||||
this.filenames = [];
|
||||
for (filename in filenames) {
|
||||
if (element.canPlayType("audio/ogg") != "" && filename.endsWith(".ogg"))
|
||||
this.filenames.push(filename);
|
||||
if (element.canPlayType("audio/mp4") != "" && filename.endsWith(".mp4"))
|
||||
this.filenames.push(filename);
|
||||
if (element.canPlayType("audio/wav") != "" && filename.endsWith(".wav"))
|
||||
this.filenames.push(filename);
|
||||
}
|
||||
|
||||
element.addEventListener("error", errorListener, false);
|
||||
element.addEventListener("canplay", canPlayThroughListener, false);
|
||||
|
||||
element.src = this.filenames[0];
|
||||
element.preload = "auto";
|
||||
element.load();
|
||||
}
|
||||
|
||||
// override public function play(): kha.SoundChannel {
|
||||
// try {
|
||||
// element.play();
|
||||
// }
|
||||
// catch (e: Dynamic) {
|
||||
// trace(e);
|
||||
// }
|
||||
// return new SoundChannel(element);
|
||||
// }
|
||||
function errorListener(eventInfo: ErrorEvent): Void {
|
||||
if (element.error.code == MediaError.MEDIA_ERR_SRC_NOT_SUPPORTED) {
|
||||
for (i in 0...filenames.length - 1) {
|
||||
if (element.src == filenames[i]) {
|
||||
// try loading with next extension:
|
||||
element.src = filenames[i + 1];
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
failed({url: element.src});
|
||||
finishAsset();
|
||||
}
|
||||
|
||||
function canPlayThroughListener(eventInfo: Event): Void {
|
||||
finishAsset();
|
||||
}
|
||||
|
||||
function finishAsset() {
|
||||
element.removeEventListener("error", errorListener, false);
|
||||
element.removeEventListener("canplaythrough", canPlayThroughListener, false);
|
||||
done(this);
|
||||
loading.remove(this);
|
||||
}
|
||||
|
||||
override public function uncompress(done: Void->Void): Void {
|
||||
done();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user