Added Audio class
This commit is contained in:
parent
38744c399c
commit
b5da68a56e
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "momentumengine",
|
||||
"version": "0.8.0",
|
||||
"version": "0.9.0",
|
||||
"description": "An ES6 game and animation engine.",
|
||||
"main": "src/es6.js",
|
||||
"repository": {
|
||||
|
68
src/classes/audio.js
Normal file
68
src/classes/audio.js
Normal file
@ -0,0 +1,68 @@
|
||||
"use strict";
|
||||
|
||||
class Audio {
|
||||
|
||||
|
||||
constructor (src) {
|
||||
|
||||
this._loaded = false; // Default is true, set it to false until the audio has loaded
|
||||
this._error = false; // If the audio fails to load, this will contain the reason
|
||||
|
||||
this._audioObj = new Audio();
|
||||
|
||||
this._audioObj.addEventListener("loadeddata", () => {
|
||||
this._loaded = true;
|
||||
this._error = false;
|
||||
});
|
||||
|
||||
this._audioObj.addEventListener("error", (err) => {
|
||||
this._loaded = false;
|
||||
this._error = err;
|
||||
});
|
||||
|
||||
this._audioObj.src = src;
|
||||
|
||||
}
|
||||
|
||||
|
||||
play () {
|
||||
if (this._loaded) {
|
||||
return this._audioObj.play();
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pause () {
|
||||
if (this._loaded) {
|
||||
return this._audioObj.pause();
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
seek (seconds) {
|
||||
if (this._loaded) {
|
||||
return this._audioObj.currentTime = seconds;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
isLoaded () {
|
||||
return this._loaded;
|
||||
}
|
||||
|
||||
|
||||
isError () {
|
||||
return this._error;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
export default Audio;
|
@ -11,6 +11,7 @@ import Path from "./classes/path.js";
|
||||
import Color from "./classes/color.js";
|
||||
import Text from "./classes/text.js";
|
||||
import Font from "./classes/font.js";
|
||||
import Audio from "./classes/audio.js";
|
||||
import ImageLoader from "./classes/imageloader.js";
|
||||
|
||||
import {KeyConsts} from "./classes/keyboardinput.js";
|
||||
@ -28,6 +29,7 @@ const Classes = {
|
||||
Color,
|
||||
Text,
|
||||
Font,
|
||||
Audio,
|
||||
ImageLoader
|
||||
};
|
||||
|
||||
|
@ -11,6 +11,7 @@ import Path from "./classes/path.js";
|
||||
import Color from "./classes/color.js";
|
||||
import Text from "./classes/text.js";
|
||||
import Font from "./classes/font.js";
|
||||
import Audio from "./classes/audio.js";
|
||||
import ImageLoader from "./classes/imageloader.js";
|
||||
|
||||
import {KeyConsts} from "./classes/keyboardinput.js";
|
||||
@ -28,6 +29,7 @@ const Classes = {
|
||||
Color,
|
||||
Text,
|
||||
Font,
|
||||
Audio,
|
||||
ImageLoader
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user