Posted By

alvincrespo on 02/18/10


Tagged

actionscript video constant stream as3 netconnection netstatus


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

TrentSkunk
Andryta


Constant Video Stream in Actionscript 3.0


 / Published in: ActionScript 3
 

This class would be used to constantly stream various videos in flash. This would be used for continuous video play.

  1. package {
  2. //display
  3. import flash.display.MovieClip;
  4.  
  5. //media
  6. import flash.media.SoundTransform;
  7. import flash.media.Video;
  8.  
  9. //net
  10. import flash.net.NetConnection;
  11. import flash.net.NetStream;
  12.  
  13. //events
  14. import flash.events.SecurityErrorEvent;
  15. import flash.events.AsyncErrorEvent;
  16. import flash.events.NetStatusEvent;
  17. import flash.events.IOErrorEvent;
  18. import flash.events.TimerEvent;
  19.  
  20. //utils
  21. import flash.utils.Timer;
  22.  
  23. //transitions
  24. import fl.transitions.Tween;
  25. import fl.transitions.easing.Strong;
  26. import fl.transitions.TweenEvent;
  27.  
  28. public class VideoManager extends MovieClip {
  29.  
  30. private var _url:String = "video/";
  31. private var _VideoList:XMLList;
  32. private var _VideoListLength:Number;
  33. private var _CurrentVideoPosition:Number = 0;
  34. private var _isPlaying:Boolean;
  35.  
  36. private var _NetConnection:NetConnection;
  37. private var _NetStream:NetStream;
  38. private var _Video:Video;
  39. private var _customClient:Object;
  40. private var _videoDuration:Number;
  41.  
  42. private var _SoundTransform:SoundTransform;
  43.  
  44. private var _VideoContainer:MovieClip;
  45.  
  46. private var _Timer:Timer;
  47.  
  48. private var _fadeOut:Tween;
  49. private var _fadeIn:Tween;
  50.  
  51. public function VideoManager(pVideoContainer:MovieClip):void {
  52. trace("Video Manager Instantiated");
  53. _VideoContainer = pVideoContainer;
  54. }//end of constructor
  55.  
  56. public function setVideoList(pVideoList:XMLList):void {
  57. trace("\nSet Video List");
  58. _VideoList = pVideoList;
  59. trace("Video List: \n" +_VideoList);
  60. _VideoListLength = _VideoList.length();
  61. trace("Video List Length: " + _VideoListLength);
  62. }//end of setVideoList
  63.  
  64. public function StartVideo(pVideoPos:Number=NaN):void {
  65.  
  66. trace("\nStart Video");
  67.  
  68. //is the position not a number
  69. if(isNaN(pVideoPos))
  70. pVideoPos = 0;
  71.  
  72. //check to see if video is playing
  73. if (_isPlaying) {
  74. //stop video
  75.  
  76. //the video is no longer playing
  77. _isPlaying = false;
  78. }
  79.  
  80. //set up SoundTransform for Video
  81. _SoundTransform = new SoundTransform();
  82. _SoundTransform.volume = 0;
  83.  
  84. //set up NetConnection
  85. _NetConnection = new NetConnection();
  86. _NetConnection.connect(null);
  87.  
  88. //set up CustomClient
  89. _customClient = new Object();
  90. _customClient.onMetaData = metaDataHandler;
  91.  
  92. //set up NetStream
  93. _NetStream = new NetStream(_NetConnection);
  94. _NetStream.client = _customClient;
  95. _NetStream.bufferTime = 1;
  96. _NetStream.receiveVideo(true);
  97. _NetStream.soundTransform = _SoundTransform;
  98.  
  99. //create instance of Video
  100. _Video = new Video(640,360);
  101. _VideoContainer.addChild(_Video);
  102. _Video.attachNetStream(_NetStream);
  103.  
  104. //Add Listeners
  105. //========Net Connection=======================
  106. _NetConnection.addEventListener(NetStatusEvent.NET_STATUS, doNetStatus);
  107. _NetConnection.addEventListener(IOErrorEvent.IO_ERROR, doIOError);
  108. _NetConnection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, doSecurityError);
  109.  
  110. //========Net Stream=======================
  111. _NetStream.addEventListener(NetStatusEvent.NET_STATUS, doNetStatus);
  112. _NetStream.addEventListener(IOErrorEvent.IO_ERROR, doIOError);
  113. _NetStream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, doAsyncError);
  114.  
  115. trace("Play Video: " + _VideoList[pVideoPos].attribute('filename'));
  116. _NetStream.play(_url+_VideoList[pVideoPos].attribute('filename'));
  117.  
  118. _fadeIn = new Tween(_Video,"alpha",Strong.easeOut,0,1,3,true);
  119. _fadeIn.addEventListener(TweenEvent.MOTION_START, fadeInStart);
  120. _fadeIn.addEventListener(TweenEvent.MOTION_STOP, fadeInStop);
  121. _fadeIn.start();
  122. }
  123.  
  124. //EVENT HANDLERS
  125. /*
  126. Example of Security Error
  127. Not having secruity rights on the server containing the FLV
  128.  
  129. Solution:
  130. crossdomain.xml file
  131. */
  132. protected function doSecurityError(evt:SecurityErrorEvent):void {
  133. trace("AbstractStream.securityError:"+evt.text);
  134. //do nothing
  135. }//end of doSecurityError
  136. /*
  137. Examples of IOErrors:
  138. connection drop
  139. loss of internet connection
  140. 404 error
  141. */
  142. protected function doIOError(evt:IOErrorEvent):void {
  143. trace("AbstractScreem.ioError:"+evt.text);
  144. //do nothing
  145. }//end of doIOError
  146. /*
  147. Examples of AsyncError
  148. usually due to streaming server
  149. */
  150. protected function doAsyncError(evt:AsyncErrorEvent) {
  151. trace("AbstractScreem.asyncError:"+evt.text);
  152. //do nothing
  153. }//end of doAsyncError
  154.  
  155. protected function doNetStatus(e:NetStatusEvent):void {
  156. switch (e.info.code) {
  157. //NetStream
  158. case "NetStream.Play.StreamNotFound":
  159. trace("Stream not found");
  160. break;
  161. case "NetStream.Play.Stop":
  162. trace("Netstream Stopped");
  163. break;
  164. case "NetStream.Play.Start":
  165. trace("Netstream Playing");
  166. break;
  167. //NetConnection
  168. case "NetConnection.Connect.Closed":
  169. trace("NetConnection Closed");
  170. //dispatchEvent(new Event(VideoStream.STREAM_COMPLETE));
  171. break;
  172. case "NetConnection.Connect.Failed":
  173. trace("NetConnection Failed");
  174. break;
  175. case "NetConnection.Connect.Success":
  176. trace("NetConnection Success");
  177. break;
  178. default :
  179. break;
  180. }
  181. }//end of doNetStatus
  182.  
  183. private function timerDone(e:TimerEvent):void{
  184. _Timer.removeEventListener(TimerEvent.TIMER, timerDone);
  185. _fadeOut = new Tween(_Video,"alpha",Strong.easeOut,1,0,3,true);
  186. _fadeOut.addEventListener(TweenEvent.MOTION_START, fadeOutStart);
  187. _fadeOut.addEventListener(TweenEvent.MOTION_STOP, fadeOutStop);
  188. _fadeOut.start();
  189. trace("Timer Done - Begin Transition Out");
  190. }
  191.  
  192. private function fadeInStart(e:TweenEvent):void{
  193. _fadeIn.removeEventListener(TweenEvent.MOTION_START, fadeInStart);
  194. }
  195. private function fadeInStop(e:TweenEvent):void{
  196. _fadeIn.removeEventListener(TweenEvent.MOTION_STOP, fadeInStop);
  197. }
  198.  
  199. private function fadeOutStart(e:TweenEvent):void{
  200. trace("Begin Transition of Next Video In");
  201. if(_CurrentVideoPosition+1 < _VideoListLength)
  202. _CurrentVideoPosition++;
  203. else
  204. _CurrentVideoPosition = 0;
  205.  
  206. trace("Current Video POS: " + _CurrentVideoPosition);
  207.  
  208. StartVideo(_CurrentVideoPosition);
  209. _fadeOut.removeEventListener(TweenEvent.MOTION_START, fadeOutStart);
  210. }
  211. private function fadeOutStop(e:TweenEvent):void{
  212. trace("Remove Everything from Previous Video");
  213. _fadeOut.removeEventListener(TweenEvent.MOTION_STOP, fadeOutStop);
  214. }
  215.  
  216. private function metaDataHandler(pObject:Object):void {
  217. for (var propName:String in pObject) {
  218. trace(propName + " = " + pObject[propName]);
  219. }
  220.  
  221. if (pObject.duration != null)
  222. _videoDuration = pObject.duration;
  223.  
  224. _Timer = new Timer((_videoDuration-2)*1000);
  225. _Timer.addEventListener(TimerEvent.TIMER, timerDone);
  226. _Timer.start();
  227. }
  228. }
  229.  
  230. }

Report this snippet  

You need to login to post a comment.