export default class {
    constructor(fileURL){
        this._fileURL = fileURL;
        this._loaded = false;
        this._buffer = null;
    }

    setup(){
        return new Promise((resolve, reject) => {
            try {
                const AudioContext = window.AudioContext || window.webkitAudioContext;
                this._context = new AudioContext();
                console.debug("audio context created");
                resolve();
            } catch (e) {
                reject(new Error("can't create AudioContext"));
                return;
            }
        }).then(() => {
            return this._loadBuffer(this._fileURL)
        }).then(() => {
            this._loaded = true;
        })
    }

    play(){
        console.debug("playing sound");
        const source = this._context.createBufferSource();
        source.buffer = this._buffer;
        source.connect(this._context.destination);
        source.start(0);
    }

    _loadBuffer(){
        const request = new XMLHttpRequest();
        request.open('GET', this._fileURL , true);
        request.responseType = 'arraybuffer';

        return new Promise((resolve, reject) => {
            request.onload = () => {
                this._context.decodeAudioData(request.response, (buffer) => {
                    this._buffer = buffer;
                    resolve();
                }, (e) => {
                    reject(e);
                });
            };
            request.send();
        })
    }
}

@@ -1,8 +1,29 @@
+let context = null;%0A%0A
 export d
@@ -114,127 +114,23 @@
 is._
-loaded = false;%0A        this._buffer = null;%0A    %7D%0A%0A    setup()%7B%0A        return new Promise((resolve, reject) =%3E %7B%0A    
+buffer = null;%0A
     
@@ -139,20 +139,16 @@
   try %7B%0A
-    
         
@@ -230,34 +230,63 @@
             
-    this._
+if (context == null) %7B%0A                
 context = ne
@@ -308,31 +308,41 @@
 );%0A         
+   %7D%0A
        
+     
 console.debu
@@ -373,39 +373,8 @@
 %22);%0A
-                resolve();%0A    
     
@@ -403,27 +403,22 @@
         
-    reject(
+throw 
 new Erro
@@ -447,17 +447,16 @@
 ontext%22)
-)
 ;%0A      
@@ -461,435 +461,27 @@
     
-        return;%0A            %7D%0A        %7D).then(() =%3E %7B%0A            return this._loadBuffer(this._fileURL)%0A        %7D).then(() =%3E %7B%0A            this._loaded = true;%0A        %7D)%0A    %7D%0A%0A    play()%7B%0A        console.debug(%22playing sound%22);%0A        const source = this._context.createBufferSource();%0A        source.buffer = this._buffer;%0A        source.connect(this._context.destination);%0A        source.start(0);%0A    %7D%0A%0A    _loadBuffer
+%7D%0A%0A    %7D%0A%0A    setup
 ()%7B%0A
@@ -727,22 +727,16 @@
         
-this._
 context.
@@ -989,16 +989,237 @@
       %7D)
+;%0A    %7D%0A%0A    play()%7B%0A        console.debug(%22playing sound%22);%0A        const source = context.createBufferSource();%0A        source.buffer = this._buffer;%0A        source.connect(context.destination);%0A        source.start(0);
 %0A    %7D%0A%7D

Support Mobile Safari