function createHTMLAudio(self, src, options) {
    var audioSrc = new Audio(src);
    var loaded = false;
    var onBuffered;

    self.audioSrc = audioSrc;
    self.stopped = true;
    self.src = src;
    self.options = options;
    self.isWebAudio = false;

    self.play = function play() {
      if (!self.stopped) return;
      self.stopped = false;

      // Wait for audio to be buffered
      if (!loaded) {
        onBuffered = function() {
          self.stopped = true;  // Need this to re-enter play()
          play(src);
        };
        self.buffer();
        return;
      }

      audioSrc.volume = options.gain;
      audioSrc.loop = !!options.loop;
      audioSrc.play();
    };

    self.stop = function stop(pause) {
      if (!loaded) return;
      self.stopped = true;
      audioSrc.pause();
      if (!pause) {
        audioSrc.currentTime = 0;
        deferredApply(self.onStop);
      }
    };

    self.pause = function pause() {
      self.stop(true);
    };

    self.buffer = function buffer() {
      audioSrc.load();
    };

    self.offset = function offset() {
      return audioSrc.duration && audioSrc.currentTime <= audioSrc.duration ?
        audioSrc.currentTime : 0;
    };

    audioSrc.addEventListener('ended', function() {
      self.stopped = true;
      self.audioSrc.currentTime = 0;
      if (!options.loop) {
        audioSrc.pause();
        deferredApply(self.onEnd);
      }
      $rootScope.$apply();
    });
    audioSrc.addEventListener('play', function() {
      deferredApply(self.onPlay);
    });
    audioSrc.addEventListener('canplaythrough', function handler() {
      audioSrc.removeEventListener('canplaythrough', handler);
      loaded = true;
      if (onBuffered) onBuffered();
      deferredApply(self.onBuffered);
    });

    return self;
  }
// Create HTML Audio source (fallback)
Create HTML Audio source (fallback)
Create HTML Audio source ( fallback )
function createHTMLAudio(self, src, options) {
    var audioSrc = new Audio(src);
    var loaded = false;
    var onBuffered;
    self.audioSrc = audioSrc;
    self.stopped = true;
    self.src = src;
    self.options = options;
    self.isWebAudio = false;
    self.play = function play() {
      if (!self.stopped) return;
      self.stopped = false;
      if (!loaded) {
        onBuffered = function() {
          self.stopped = true;  
          play(src);
        };
        self.buffer();
        return;
      }
      audioSrc.volume = options.gain;
      audioSrc.loop = !!options.loop;
      audioSrc.play();
    };
    self.stop = function stop(pause) {
      if (!loaded) return;
      self.stopped = true;
      audioSrc.pause();
      if (!pause) {
        audioSrc.currentTime = 0;
        deferredApply(self.onStop);
      }
    };
    self.pause = function pause() {
      self.stop(true);
    };
    self.buffer = function buffer() {
      audioSrc.load();
    };
    self.offset = function offset() {
      return audioSrc.duration && audioSrc.currentTime <= audioSrc.duration ?
        audioSrc.currentTime : 0;
    };
    audioSrc.addEventListener('ended', function() {
      self.stopped = true;
      self.audioSrc.currentTime = 0;
      if (!options.loop) {
        audioSrc.pause();
        deferredApply(self.onEnd);
      }
      $rootScope.$apply();
    });
    audioSrc.addEventListener('play', function() {
      deferredApply(self.onPlay);
    });
    audioSrc.addEventListener('canplaythrough', function handler() {
      audioSrc.removeEventListener('canplaythrough', handler);
      loaded = true;
      if (onBuffered) onBuffered();
      deferredApply(self.onBuffered);
    });
    return self;
  }
function createHTMLAudio ( self , src , options ) { var audioSrc = new Audio ( src ) ; var loaded = false ; var onBuffered ; self . audioSrc = audioSrc ; self . stopped = true ; self . src = src ; self . options = options ; self . isWebAudio = false ; self . play = function play ( ) { if ( ! self . stopped ) return ; self . stopped = false ; if ( ! loaded ) { onBuffered = function ( ) { self . stopped = true ; play ( src ) ; } ; self . buffer ( ) ; return ; } audioSrc . volume = options . gain ; audioSrc . loop = ! ! options . loop ; audioSrc . play ( ) ; } ; self . stop = function stop ( pause ) { if ( ! loaded ) return ; self . stopped = true ; audioSrc . pause ( ) ; if ( ! pause ) { audioSrc . currentTime = 0 ; deferredApply ( self . onStop ) ; } } ; self . pause = function pause ( ) { self . stop ( true ) ; } ; self . buffer = function buffer ( ) { audioSrc . load ( ) ; } ; self . offset = function offset ( ) { return audioSrc . duration && audioSrc . currentTime <= audioSrc . duration ? audioSrc . currentTime : 0 ; } ; audioSrc . addEventListener ( 'ended' , function ( ) { self . stopped = true ; self . audioSrc . currentTime = 0 ; if ( ! options . loop ) { audioSrc . pause ( ) ; deferredApply ( self . onEnd ) ; } $rootScope . $apply ( ) ; } ) ; audioSrc . addEventListener ( 'play' , function ( ) { deferredApply ( self . onPlay ) ; } ) ; audioSrc . addEventListener ( 'canplaythrough' , function handler ( ) { audioSrc . removeEventListener ( 'canplaythrough' , handler ) ; loaded = true ; if ( onBuffered ) onBuffered ( ) ; deferredApply ( self . onBuffered ) ; } ) ; return self ; }
Create HTML Audio source (fallback)
Create HTML Audio source ( fallback )
// Wait for audio to be buffered // Need this to re-enter play()