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

File: media/LMedia.js

/** @language japanese
 * <p>LMedia クラスは、LSoundオブジェクトとLVideoオブジェクトの基本クラスです。</p>
 * <p>LMedia は抽象基本クラスであるため、LMedia を直接呼び出すことはできません。</p>
 * <p>LMedia クラスは、子オブジェクトを持つすべてのオブジェクトの抽象基本クラスです。これは直接インスタンス化することはできません。</p>
 * @class LMedia
 * @extends LDisplayObject
 * @constructor
 * @since 1.7.0
 * @public
 */
var LMedia = (function () {
	function LMedia () {
		var s = this;
		LExtends(s, LDisplayObject, []);
		/** @language japanese
		 * LSoundオブジェクトまたはLVideoオブジェクトの長さ
		 * @property length
		 * @type int
		 * @since 1.7.0
		 * @public
		 */
		s.length = 0;
		s.loopIndex = 0;
		s.loopLength = 1;
		/** @language japanese
		 * LSoundオブジェクトまたはLVideoオブジェクトは再生中かどうか
		 * @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 japanese
		 * <p>指定した URL から外部 メディア ファイルのロードを開始します。</p>
		 * <p>複数のブラウザをサポートするため、下記のように、複数なファイルを使うことができます。</p>
		 * <p>medio.load("medias/a.mp3,medias/a.wav,medias/a.ogg");</p>
		 * @method load
		 * @param {String} url 外部の メディア ファイルを指定する URL です。
		 * @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 japanese
		 * <p>再生した時間を取得。</p>
		 * @method getCurrentTime
		 * @return {int} 再生した時間。
		 * @since 1.7.0
		 * @public
		 */
		getCurrentTime : function () {
			return this.data.currentTime;
		},
		/** @language japanese
		 * <p>音量変更。</p>
		 * @method setVolume
		 * @param {float} value 音量。
		 * @since 1.7.0
		 * @public
		 */
		setVolume : function (v) {
			this.data.volume = v;
		},
		/** @language japanese
		 * <p>音量を取得。</p>
		 * @method getVolume
		 * @return {float} 音量。
		 * @since 1.7.0
		 * @public
		 */
		getVolume : function () {
			return this.data.volume;
		},
		/** @language japanese
		 * <p>メディアオブジェクトを再生する。</p>
		 * @method play
		 * @param {float} startTime 再生を開始する初期位置(ミリ秒単位)です。
		 * @param {int} loops サウンドチャネルの再生が停止するまで startTime 値に戻ってサウンドの再生を繰り返す回数を定義します。
		 * @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 japanese
		 * <p>指定する長さの音声オブジェクトを再生する。</p>
		 * @method playSegment
		 * @param {float} startTime 再生を開始する初期位置(秒単位)です。
		 * @param {float} segment 指定する長さ(秒単位)。
		 * @param {int} loops サウンドチャネルの再生が停止するまで startTime 値に戻ってサウンドの再生を繰り返す回数を定義します。
		 * @since 1.9.0
		 * @public
		 */
		playSegment : function (c, seg, l) {
			this.playTo(c, c + seg, l);
		},
		/** @language japanese
		 * <p>指定する長さの音声オブジェクトを再生する。</p>
		 * @method playTo
		 * @param {float} startTime 再生を開始する初期位置(秒単位)です。
		 * @param {float} endTime 再生を終了する位置(秒単位)。
		 * @param {int} loops サウンドチャネルの再生が停止するまで startTime 値に戻ってサウンドの再生を繰り返す回数を定義します。
		 * @since 1.9.0
		 * @public
		 */
		playTo : function (c, to, l) {
			this.play(c, l, to);
		},
		/** @language japanese
		 * <p>再生を一時停止する。</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 japanese
		 * <p>再生を一時クローズする。</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 japanese
 * audioまたはvideoファイルロード完了。
 * <p><a href="LEvent.html#property_COMPLETE">LEvent.COMPLETE</a></p>
 * @event LEvent.COMPLETE
 */