aboutsummaryrefslogtreecommitdiff
path: root/src/js/AudioPlayer.js
diff options
context:
space:
mode:
authorIndrajith K L2018-08-14 11:05:32 +0530
committerIndrajith K L2018-08-14 11:05:32 +0530
commitecc12b2ef3b058ddd8bf9612afd7c9276f0386f9 (patch)
tree7eeeaa2644740920c07b7cfbc77d2a84eb68bf83 /src/js/AudioPlayer.js
parente1f9fdc2c83d0b15286aeb8f39be34e8612e2ec3 (diff)
downloadjs13kgames-template-ecc12b2ef3b058ddd8bf9612afd7c9276f0386f9.tar.gz
js13kgames-template-ecc12b2ef3b058ddd8bf9612afd7c9276f0386f9.tar.bz2
js13kgames-template-ecc12b2ef3b058ddd8bf9612afd7c9276f0386f9.zip
* removed unwanted dependencies
* improved compression * Introduces Audio Player :sparkles: :fire: :racehorse: :snowflake: :gem:
Diffstat (limited to 'src/js/AudioPlayer.js')
-rw-r--r--src/js/AudioPlayer.js25
1 files changed, 25 insertions, 0 deletions
diff --git a/src/js/AudioPlayer.js b/src/js/AudioPlayer.js
new file mode 100644
index 0000000..952f487
--- /dev/null
+++ b/src/js/AudioPlayer.js
@@ -0,0 +1,25 @@
+export default class AudioPlayer{
+ constructor(){
+ this.audioPlayer = new Audio();
+ this.url = window.URL || window.webkitURL;
+ this.soundURL = null;
+ this.attachEventListeners();
+ }
+
+ attachEventListeners(){
+ this.audioPlayer.addEventListener('error', (e)=> {
+ console.log("Error: " + e);
+ }, false);
+ this.audioPlayer.addEventListener('ended', (e)=> {
+ this.audioPlayer = null;
+ this.url.revokeObjectURL(this.soundURL);
+ }, false);
+ }
+
+ play(soundUrl){
+ this.soundURL = soundUrl;
+ this.audioPlayer.src = this.soundURL;
+ this.audioPlayer.play();
+
+ }
+} \ No newline at end of file