Looping Progressively Downloaded .flv Files
I’ve seen this asked several times in as many different forums, so thought I’d try to help with a little class I made a few months ago.
/** * Creates an automatically looping video. * @author Devon O. * @version 0.1 */ package com.onebyonedesign.extras { import flash.media.Video; import flash.net.NetConnection; import flash.net.NetStream; import flash.events.NetStatusEvent; import flash.events.AsyncErrorEvent; public class VideoLoop extends Video { private var URL:String; private var connection:NetConnection private var stream:NetStream; private var playedOnce:Boolean = false; /** * * @param relative or absolute path to .flv file */ public function VideoLoop(videoPath:String):void { URL = videoPath; connection = new NetConnection(); connection.addEventListener(NetStatusEvent.NET_STATUS, onStatus); connection.connect(null); } public function play():void { stream.resume(); } public function pause():void { stream.pause(); } private function onStatus(nse:NetStatusEvent):void { if (nse.info.code == "NetConnection.Connect.Success" && !playedOnce){ playedOnce = true; initVideo(); } if (nse.info.code == "NetStream.Play.Stop" && playedOnce) { stream.seek(0); } } private function asyncErrorHandler(event:AsyncErrorEvent):void { dispatchEvent(event); } private function initVideo():void { stream = new NetStream(connection); stream.addEventListener(NetStatusEvent.NET_STATUS, onStatus); stream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler); attachNetStream(stream); stream.play(URL); } } }
Basic usage is easy as pie. Just create a new instance of the VideoLoop class passing the path to your video as a parameter then add that instance to your display list.
Here’s a quick example:
package { import com.onebyonedesign.extras.VideoLoop; import flash.display.Sprite; import flash.events.AsyncErrorEvent; import flash.events.Event; import flash.events.MouseEvent; import flash.text.AntiAliasType; import flash.text.TextField; import flash.text.TextFieldAutoSize; import flash.text.TextFormat; import flash.text.TextFormatAlign; public class Main extends Sprite { private var isPlaying:Boolean = true; private var videoLoop:VideoLoop; public function Main():void { videoLoop = new VideoLoop("water.flv"); videoLoop.addEventListener(AsyncErrorEvent.ASYNC_ERROR, onAsyncError); addChild(videoLoop); stage.addEventListener(MouseEvent.CLICK, adjustVideo); var info:TextField = new TextField(); info.selectable = false; info.autoSize = TextFieldAutoSize.LEFT; info.antiAliasType = AntiAliasType.ADVANCED; var fmt:TextFormat = new TextFormat("_sans", 12, 0x939393); fmt.align = TextFormatAlign.CENTER; info.defaultTextFormat = fmt; info.text = "Two second video looped.\nClick to pause/resume."; info.x = 95; info.y = 190; addChild(info); } private function onAsyncError(aee:AsyncErrorEvent):void { // handle annoying async errors (such as the missing metadata property) here. } private function adjustVideo(me:MouseEvent):void { if (isPlaying) { videoLoop.pause(); } else { videoLoop.play(); } isPlaying = !isPlaying; } } }
Which produces the following:
[kml_flashembed movie=”../wp-content/uploads/2008/02/loop_vid.swf” height=”240″ width=”320″ fversion=”9″ /]