Code coverage report for api/audio_video/AudioPlayer.js

Statements: 6.06% (4 / 66)      Branches: 0% (0 / 44)      Functions: 0% (0 / 13)      Lines: 6.06% (4 / 66)      Ignored: none     

All files » api/audio_video/ » AudioPlayer.js
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190    1                     1                           1                                                                                                                                                                                                                                                                                                                                 1  
/* globals document, Media */
 
var HTML5Audio = require("./HTML5Audio").HTML5Audio,
  CordovaAudio = require("./HTML5Audio").HTML5Audio;
 
/**
 * @class AudioPlayer is a minimal customization of the HTML5 media controller
 *
 * @name AudioPlayer
 *
 * @extends Object
 * @constructs
 */
var AudioPlayer = function AudioPlayer(options) {
  if (!this._fieldDBtype) {
    this._fieldDBtype = "AudioPlayer";
  }
  if (this.options) {
    console.warn("AudioPlayer was created with options but it doesnt accept options", options);
  }
  // console.log(HTML5Audio);
  // console.log(CordovaAudio.play);
  this.mediaController = new HTML5Audio();
 
  Object.apply(this, arguments);
};
 
AudioPlayer.prototype = Object.create(Object.prototype, /** @lends AudioPlayer.prototype */ {
  constructor: {
    value: AudioPlayer
  },
 
  isPlaying: {
    configurable: true,
    get: function() {
      return this.mediaController.isPlaying;
    }
  },
 
  isPaused: {
    configurable: true,
    get: function() {
      return this.mediaController.isPaused;
    }
  },
 
  audioPlayStartTime: {
    configurable: true,
    get: function() {
      if (this.mediaController.audioPlayStarted) {
        return this.mediaController.audioPlayStarted;
      } else {
        return 0;
      }
    }
  },
 
  isCordova: {
    configurable: true,
    get: function() {
      // return false;
      try {
        if (!Media) {
          console.log("We are most likely in Cordova, using Cordova instead of HTML5 audio");
        }
        return true;
      } catch (e) {
        console.log("We are most likely not in Cordova, using HTML5 audio");
        return false;
      }
    }
  },
 
  getDuration: {
    configurable: true,
    value: function(src) {
      if (src && this.src.indexOf(src) > -1 && this.mediaController.src.indexOf(src) > -1) {
        return this.mediaController.duration || 0;
      } else {
        console.log("Duration wasn't clear, so returning 0");
        return 0;
      }
    }
  },
 
  src: {
    configurable: true,
    get: function() {
      return this._src;
    },
    set: function(value) {
      if (value && value.trim() && value.trim() === this._src) {
        return;
      }
      console.log("Changed audio source: " + value);
      if (this.isCordova && this.mediaController.library !== "Cordova") {
        this._src = value;
        this.mediaController = CordovaAudio;
      } else {
        try {
          if (!value.match(/^[^:]+:\/\//)) {
            this._src = document.location.href.replace(document.location.pathname, "/" + value);
          } else {
            this._src = value;
          }
          if (!this.mediaController._audioElement) {
            //Try to use the full path to the audio file if its a relative path
            if (document.getElementById(this._src)) {
              this.mediaController._audioElement = document.getElementById(this._src);
            } else {
              var audio = document.createElement("audio");
              audio.setAttribute("id", this._src);
              //todo set hidden?
              document.body.appendChild(audio);
              this.mediaController._audioElement = audio;
            }
          }
        } catch (e) {
          this.warn("There is no DOM, cant set audio src");
          this.debug(e);
        }
      }
 
      this.mediaController.src = this._src;
      console.log("Set the src in core/audio-player " + this._src);
    }
  },
 
  play: {
    configurable: true,
    value: function(optionalSource, optionalDelay) {
      if (optionalSource) {
        this.src = optionalSource;
      }
      if (this.mediaController) {
        console.log("this.mediaController.play " + this._src);
        this.mediaController.play(this._src, optionalDelay);
      } else {
        console.log("couldnt play " + this._src);
      }
    }
  },
 
  pause: {
    configurable: true,
 
    value: function() {
      if (this.mediaController) {
        this.mediaController.pause();
      }
    }
  },
 
  togglePause: {
    configurable: true,
    value: function() {
      console.log("togglePause");
      if (this.mediaController) {
        if (this.mediaController.isPaused) {
          console.log("   playing");
          this.mediaController.play();
        } else {
          console.log("   paused");
          this.mediaController.pause();
        }
      }
    }
  },
 
  stop: {
    configurable: true,
    value: function() {
      if (this.mediaController) {
        this.mediaController.stop();
      }
    }
  },
 
  addEvent: {
    configurable: true,
    value: function(message, startTime, endTime) {
      if (this.mediaController) {
        this.mediaController.addAudioEventAtTimePeriod(message, startTime, endTime);
      }
    }
  }
});
 
exports.AudioPlayer = AudioPlayer;