API Docs for: 最后更新日期:2014年8月25日
Google搜索   
Show:

File: media/LMedia.js

/** @language english
 * <p>The LMedia class is the base class for LSound object and LVideo object.</p>
 * <p>LMedia is an abstract base class; therefore, you cannot call LMedia directly. </p>
 * <p>The LMedia class is an abstract base class for all objects that can contain child objects. It cannot be instantiated directly.</p>
 * @class LMedia
 * @extends LDisplayObject
 * @constructor
 * @since 1.7.0
 * @public
 */
var LMedia = (function () {
	function LMedia () {
		var s = this;
		LExtends(s, LDisplayObject, []);
		/** @language english
		 * The length of LSound object or LVideo object
		 * @property length
		 * @type int
		 * @since 1.7.0
		 * @public
		 */
		s.length = 0;
		s.loopIndex = 0;
		s.loopLength = 1;
		/** @language english
		 * The LSound object or LVideo object is playing.
		 * @property playing
		 * @type Boolean
		 * @since 1.7.0
		 * @public
		 */
		s.playing = false;
		s.oncomplete = null;
		s.onsoundcomplete = null;
		s.currentStart = 0;
		LSound.Container.add(this);
	}
	var p = {
		onload : function () {
			var s = this;
			if (s.data.readyState) {
				s.length = s.data.duration - (LGlobal.android ? 0.1 : 0);
				var e = new LEvent(LEvent.COMPLETE);
				e.currentTarget = s;
				e.target = s.data;
				s.dispatchEvent(e);
				return;
			}
			s.data.addEventListener("canplaythrough", function () {
				s.onload();
			}, false);
		},
		_onended : function () {
			var s = this, i, l;
			if (s.data.ended) {
				s.dispatchEvent(LEvent.SOUND_COMPLETE);
			}
			if (++s.loopIndex < s.loopLength) {
				i = s.loopIndex;
				l = s.loopLength;
				s.close();
				s.play(s.currentStart, s.loopLength, s.currentTimeTo);
				s.loopIndex = i;
			} else {
				s.close();
			}
		},
		/** @language english
		 * <p>Initiates loading of an external media file from the specified URL. </p>
		 * <p>To support different browsers, you can look like this</p>
		 * <p>medio.load("medias/a.mp3,medias/a.wav,medias/a.ogg");</p>
		 * @method load
		 * @param {String} url A URL that points to an external media file.
		 * @since 1.7.0
		 * @public
		 */
		load : function (u) {
			var s = this;
			if (Object.prototype.toString.apply(u) == "[object HTMLAudioElement]") {
				s.data = u;
				s.onload();
				return;
			}
			var a, b, c, k, d, q = {"mov" : ["quicktime"], "3gp" : ["3gpp"], "ogv" : ["ogg"], "m4a" : ["mpeg"], "mp3" : ["mpeg"], "wav" : ["wav", "x-wav", "wave"], "wave" : ["wav", "x-wav", "wave"], "aac" : ["mp4"]};
			a = u.split(',');
			for (k in a) {
				b = a[k].split('.');
				d = b[b.length - 1];
				if (q[d]) {
					d = q[d];
				} else {
					d = [d];
				}
				c = d.some(function (element, index, array) {
					return s.data.canPlayType(s._type + "/" + element);
				});
				if (c) {
					s.data.src = a[k];
					s.onload();
					s.data.load();
					return;
				}
			}
			if (s.oncomplete) {
				s.oncomplete({});
			}
		},
		/** @language english
		 * <p>Get the time of reproducing.</p>
		 * @method getCurrentTime
		 * @return {int} the time of reproducing.
		 * @since 1.7.0
		 * @public
		 */
		getCurrentTime : function () {
			return this.data.currentTime;
		},
		/** @language english
		 * <p>Set the volume of media.</p>
		 * @method setVolume
		 * @param {float} value the volume。
		 * @since 1.7.0
		 * @public
		 */
		setVolume : function (v) {
			this.data.volume = v;
		},
		/** @language english
		 * <p>Get the volume of media.</p>
		 * @method getVolume
		 * @return {float} the volume.
		 * @since 1.7.0
		 * @public
		 */
		getVolume : function () {
			return this.data.volume;
		},
		/** @language english
		 * <p>play the media file.</p>
		 * @method play
		 * @param {float} startTime The initial position in milliseconds at which playback should start.
		 * @param {int} loops Defines the number of times a sound loops back to the startTime value before the sound channel stops playback.
		 * @since 1.7.0
		 * @public
		 */
		play : function (c, l, to) {
			var s = this;
			if (s.length == 0) {
				return;
			}
			if (typeof c != UNDEFINED) {
				s.data.currentTime = c;
				s.currentStart = c;
			}
			if (typeof l != UNDEFINED) {
				s.loopLength = l;
			}
			if (typeof to !== UNDEFINED) {
				s.currentTimeTo = to > s.length ? s.length : to;
			} else {
				s.currentTimeTo = s.length;
			}
			if (s.timeout) {
				clearTimeout(s.timeout);
				delete s.timeout;
			}
			s.timeout = setTimeout(function(){
				s._onended();
			}, (s.currentTimeTo - s.data.currentTime) * 1000);
			s.data.loop = false;
			s.loopIndex = 0;
			s.playing = true;
			s.data.play();
		},
		/** @language english
		 * <p>play segment of audio.</p>
		 * @method playSegment
		 * @param {float} startTime The initial position in milliseconds at which playback should start.
		 * @param {float} segment length。
		 * @param {int} loops Defines the number of times a sound loops back to the startTime value before the sound channel stops playback.
		 * @since 1.9.0
		 * @public
		 */
		playSegment : function (c, seg, l) {
			this.playTo(c, c + seg, l);
		},
		/** @language english
		 * <p>play segment of audio.</p>
		 * @method playTo
		 * @param {float} startTime start time.
		 * @param {float} endTime end time.
		 * @param {int} loops Defines the number of times a sound loops back to the startTime value before the sound channel stops playback.
		 * @since 1.9.0
		 * @public
		 */
		playTo : function (c, to, l) {
			this.play(c, l, to);
		},
		/** @language english
		 * <p>Pauses the currently playing audio/video.</p>
		 * @method stop
		 * @since 1.7.0
		 * @public
		 */
		stop : function () {
			var s = this;
			if (!s.playing) {
				return;
			}
			if (s.timeout) {
				clearTimeout(s.timeout);
				delete s.timeout;
			}
			s.playing = false;
			s.data.pause();
		},
		/** @language english
		 * <p>Close the currently playing audio/video.</p>
		 * @method close
		 * @since 1.7.0
		 * @public
		 */
		close : function () {
			var s = this;
			if (!s.playing) {
				return;
			}
			if (s.timeout) {
				clearTimeout(s.timeout);
				delete s.timeout;
			}
			s.playing = false;
			s.data.pause();
			s.data.currentTime = 0;
			s.currentSave = 0;
		},
		ll_check : function () {
			var s = this;
			if (!s.playing) {
				return;
			}
			if (s.currentTimeTo < s.data.currentTime + LSound.Container.time * 0.005) {
				s._onended();
			}
		},
		die : function () {
			LSound.Container.remove(this);
		}
	};
	for (var k in p) {
		LMedia.prototype[k] = p[k];
	}
	return LMedia;
})();
/** @language english
 * when the media is loaded
 * <p><a href="LEvent.html#property_COMPLETE">LEvent.COMPLETE</a></p>
 * @event LEvent.COMPLETE
 */