aboutsummaryrefslogtreecommitdiff
path: root/src/js/AudioPlayer.js
blob: 952f487ee35e72e39ae9946b6191f0d7d5541dfa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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();

    }
}