たくさんのFLVをいかに効率よく、美しく流すか。
みたいなメモです。

– 前提
Video、NetStreamやNetConnectionなどの処理を
まとめたVideoSpriteクラス、みたいなものを作る。
これらのインスタンスを毎回消去して、新しくnewすると
ブラウザに負荷がかかるので、消去せずに再利用する。

– 再利用と消去
以下に載せたクラスで、再利用前にやるのがsleepメソッド。
本当にいらなくなった時に消去するために呼ぶのがkillメソッド。

– ノイズ防止
FLVはキーフレーム間を差分で描画しているので、無茶な処理をすると
ピンクのノイズが出て、描画がボロボロになります。その対策。

1. 一度playを実行した_ns(NetStream)に再度playをかける時は、
必ずthis._ns.close();を呼んでから。

2. 再生中のものを頭出ししてすぐ再生したい時は
pause(); → seek(0); → NetStatusEventのNetStream.Seek.Notifyを受ける。
→ resume();
(単純に1のように、closeして再度playでもいい気がします)

— 追記(08/05/20):
再生中のものはすでにキャッシュ済みなのですぐ再生可能
→ 単純に1のように、closeして再度playした方が良いです。
—-

まとめると、closeし忘れに注意ということです。

以下 as。

package{

	import flash.display.Sprite;
	import flash.events.NetStatusEvent;
	import flash.media.Video;
	import flash.net.NetConnection;
	import flash.net.NetStream;

	public class VideoSprite extends Sprite

		private var _video:Video;
		private var _nc:NetConnection;
		private var _ns:NetStream;

		public function VideoSprite():void{
			this._video = new Video();
			this.addChild(this._video);
			//NetConnectionをnew
			this._nc = new NetConnection();
			this._nc.connect(null);
			//NetStreamをnew
			this._ns = new NetStream(this._nc);
			var customClient = new Object();
			this._ns.client = customClient;
			//NetStreamをattach
			this._video.attachNetStream(this._ns);
			//イベント定義
			this._ns.addEventListener(NetStatusEvent.NET_STATUS, this.statusHandler); 
		}

		//NetStatusEvent処理
		private function statusHandler(event:NetStatusEvent):void
		{
			switch(event.info.code) {
				// 省略
				default:
					break;
			}
		}

		//色々省略

		//再利用時のクローズ作業
		public function sleep():void{
			this._video.clear();
			this._nc.close();
			this._ns.close();
		}

		//消去時
		public function kill():void {
			this.sleep();
			this.removeChild(this._video);
			this._ns.removeEventListener(NetStatusEvent.NET_STATUS, this.statusHandler); 
			this._video.attachNetStream(null);
			this._ns = null;
			this._nc = null;
			this._customClient = null;
			this._video = null;
		}
	}

}