Added Audio class

This commit is contained in:
Nathan Kunicki 2017-02-24 16:49:17 -06:00
parent 38744c399c
commit b5da68a56e
4 changed files with 73 additions and 1 deletions

View File

@ -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
View 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;

View File

@ -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
};

View File

@ -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
};