AS3 NetConnectShare Class


/ Published in: ActionScript 3
Save to your folder(s)

This can be used to help load, play and monitor playback of external video.


Copy this code and paste it in your HTML
  1. package
  2. {
  3. import flash.display.Sprite;
  4. import flash.events.Event;
  5. import flash.events.NetStatusEvent;
  6. import flash.events.SecurityErrorEvent;
  7. import flash.events.TimerEvent;
  8. import flash.media.SoundTransform;
  9. import flash.media.Video;
  10. import flash.net.NetConnection;
  11. import flash.net.NetStream;
  12. import flash.utils.Timer;
  13.  
  14. public class NetConnectShare extends Sprite
  15. {
  16. private var videoURL:String = null;
  17. public static var connection:NetConnection = new NetConnection();
  18. public var stream:NetStream;
  19. private static var tim:Timer;
  20. private var muted:Boolean = false;
  21. private var previousPlayheadTime:Number;
  22. private static var pause:Boolean = false;
  23. private var currentVolume:Number = 1;
  24. private var cuePoints:Boolean = false;
  25. private var ASCuePoints:Array = new Array();
  26. private var intBad:int = 0;
  27.  
  28. public var hasStream:Boolean = false;
  29. public var video:Video;
  30.  
  31.  
  32. public function NetConnectShare( _video:Video )
  33. {
  34. // set the output
  35. video = _video;
  36. // create listeners for connection
  37. connection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
  38. connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
  39. }
  40.  
  41.  
  42. public function connect( _videoURL:String, _video:Video = null ):void
  43. {
  44. // set the source url
  45. videoURL = _videoURL;
  46.  
  47. if (_video != null)
  48. {
  49. video = _video;
  50. }
  51.  
  52. // create connection
  53. connection.connect(null);
  54. }
  55.  
  56.  
  57. public function switchPlayers( _video:Video ):void
  58. {
  59. trace("[switchPlayers]", "net" );
  60. // assign output and attatch stream
  61. video = _video;
  62. video.attachNetStream(stream);
  63. }
  64.  
  65.  
  66. public function playheadTime():Number
  67. {
  68. trace("[playheadTime]", "playheadTime" );
  69. if (stream)
  70. {
  71. return stream.time;
  72. }
  73. else
  74. {
  75. return 0;
  76. }
  77. }
  78.  
  79.  
  80. public function play( url:String ):void
  81. {
  82. trace( "[play] pause = false", "net" );
  83. // assign source and play stream
  84. videoURL = url;
  85. playStream();
  86.  
  87. pause = false;
  88. }
  89.  
  90.  
  91. public function stop():void
  92. {
  93. trace( "[stop] pause = true", "net" );
  94. // stop timer and stream
  95. if (tim)
  96. {
  97. tim.stop();
  98. }
  99. stream.pause();
  100. pause = true;
  101. }
  102.  
  103.  
  104. public function pausePlay( e:Event = null ):void
  105. {
  106. pause = ! pause;
  107. trace( "[pausePlay] pause = " + String( pause ) + " isEvent = " + String( Boolean( e ) ) , "net" );
  108. //toggle pause / play and if pausing stop timer, if playing start or create timer
  109. if (pause)
  110. {
  111. trace( "[pausePlay] STOP " + String( tim ) + " pause = " + String( pause ) , "net" );
  112. if (tim)
  113. {
  114. tim.stop();
  115. }
  116. }
  117. else
  118. {
  119. if (tim)
  120. {
  121. trace( "[pausePlay] START " + String( tim ) + " pause = " + String( pause ) , "net" );
  122. tim.start();
  123. }
  124. else
  125. {
  126. trace( "[pausePlay] NEW TIM pause = " + String( pause ) , "net" );
  127. try
  128. {
  129. tim.stop();
  130. trace("[pausePlay] old tim ");
  131. }
  132. catch (e:Error)
  133. {
  134. trace("[pausePlay] old tim error");
  135. }
  136. tim = new Timer(500);
  137. tim.addEventListener(TimerEvent.TIMER, timeCheck );
  138. tim.start();
  139. }
  140. }
  141. try
  142. {
  143. stream.togglePause();
  144. }
  145. catch (e:Error)
  146. {
  147. trace( "[pausePlay] ERROR \n" + e );
  148. }
  149. }
  150.  
  151.  
  152. public function playOn( e:Event = null ):void
  153. {
  154. trace( "[playOn] pause = " + String( pause ) + " isEvent = " + String( Boolean( e ) ) , "net" );
  155. // make sure stream is pause and togglePause to play
  156. if (! pause)
  157. {
  158. if (tim)
  159. {
  160. trace( "[playOn] START " + String( tim ) + " pause = " + String( pause ) , "net" );
  161. tim.start();
  162. stream.pause();
  163. try
  164. {
  165. stream.togglePause();
  166. }
  167. catch (e:Error)
  168. {
  169. trace( "[pausePlay] ERROR \n" + e );
  170. }
  171. }
  172. else
  173. {
  174. trace( "[playOn] NEW TIM pause = " + String( pause ) , "net" );
  175. stream.pause();
  176. try
  177. {
  178. stream.togglePause();
  179. }
  180. catch (e:Error)
  181. {
  182. trace( "[pausePlay] ERROR \n" + e );
  183. }
  184. tim = new Timer(500);
  185. tim.addEventListener(TimerEvent.TIMER, timeCheck );
  186. tim.start();
  187. }
  188. }
  189. }
  190.  
  191.  
  192. public function seek( num:Number ):void
  193. {
  194. trace( "[seek]", "net" );
  195. stream.seek( num );
  196. }
  197.  
  198.  
  199. public function set volume( num:Number ):void
  200. {
  201. trace("[volume]", "net" );
  202. var sT:SoundTransform = new SoundTransform(num);
  203.  
  204. stream.soundTransform = sT;
  205. currentVolume = num;
  206. }
  207.  
  208.  
  209. public function get volume():Number
  210. {
  211. return currentVolume;
  212. }
  213.  
  214.  
  215. public function mute( e:Event = null ):void
  216. {
  217. var sT:SoundTransform;
  218. // toggle mute
  219. if (muted)
  220. {
  221. sT = new SoundTransform(1,0);
  222. stream.soundTransform = sT;
  223. currentVolume = 1;
  224. }
  225. else
  226. {
  227. sT = new SoundTransform(0,0);
  228. stream.soundTransform = sT;
  229. currentVolume = 0;
  230. }
  231. muted = ! muted;
  232. trace("[mute] "+ String( muted ) , "net" );
  233. }
  234.  
  235.  
  236. public function isMuted():Boolean
  237. {
  238. return muted;
  239. }
  240.  
  241.  
  242. public function get source():String
  243. {
  244. return this.videoURL;
  245. }
  246.  
  247.  
  248. public function set source( _videoURL:String ):void
  249. {
  250. videoURL = _videoURL;
  251. }
  252.  
  253.  
  254. public function addASCuePoint( _time:Number, name:String ):void
  255. {
  256. var tArr:Array = new Array();
  257. // add cue point as multidymentional array
  258. // [ timeOfCuePoin, nameOfCuePoint, hasCuePointBeenPassed ]
  259. tArr = [_time,name,false];
  260. cuePoints = true;
  261. ASCuePoints.push( tArr );
  262. }
  263.  
  264.  
  265. public function get bytesLoaded():Number
  266. {
  267. return stream.bytesLoaded;
  268. }
  269.  
  270.  
  271. public function get bytesTotal():Number
  272. {
  273. return stream.bytesTotal;
  274. }
  275.  
  276.  
  277. public function get totalTime():Number
  278. {
  279. return stream.client.duration;
  280. }
  281.  
  282.  
  283. public function reConnectStream():void
  284. {
  285. trace( "[reConnectStream]", "net" );
  286. stream = new NetStream(connection);
  287. stream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
  288. stream.client = new CustomClient();
  289. video.attachNetStream(stream);
  290.  
  291. playStream();
  292. }
  293.  
  294.  
  295. // Private Methods
  296.  
  297. private function netStatusHandler(event:NetStatusEvent):void
  298. {
  299. switch (event.info.code)
  300. {
  301. case "NetConnection.Connect.Success" :
  302. connectStream();
  303. break;
  304. case "NetStream.Play.StreamNotFound" :
  305. trace("Stream not found: " + videoURL + "\n" , "net" );
  306. break;
  307. }
  308. }
  309.  
  310.  
  311. private function securityErrorHandler(event:SecurityErrorEvent):void
  312. {
  313. trace("securityErrorHandler: " + event);
  314. }
  315.  
  316.  
  317. private function connectStream():void
  318. {
  319. trace( "[connectStream]", "net" );
  320. if (! this.hasStream && videoURL)
  321. {
  322. trace("SNAP "+ String( hasStream ) +" "+ String( videoURL ), "net" );
  323. this.hasStream = true;
  324. stream = new NetStream(connection);
  325. stream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
  326. stream.client = new CustomClient();
  327. video.attachNetStream(stream);
  328.  
  329. playStream();
  330. }
  331. }
  332.  
  333.  
  334. private function playStream():void
  335. {
  336. trace("[playStream] pause = false", "net" );
  337. intBad = 0;
  338. stream.client.metaRec = false;
  339. stream.play(videoURL);
  340. pause = false;
  341.  
  342. tim = new Timer(500);
  343. tim.addEventListener(TimerEvent.TIMER, timeCheck );
  344. tim.start();
  345. }
  346.  
  347.  
  348. private function timeCheck( e:TimerEvent ):void
  349. {
  350. trace("[timeCheck] pause = " + String( pause ), "net");
  351. var i:uint;
  352. var myPlayheadTime:Number = this.playheadTime();
  353.  
  354. // end video if at or beyond the meta data duration
  355. if (stream.client.metaRec)
  356. {
  357. if ( ( myPlayheadTime >= stream.client.duration ) )
  358. {
  359. trace("MetaRec: " + "\n", "net" );
  360. stream.client.onPlayStatus();
  361. stream.close();
  362. this.dispatchEvent( new Event ( "videoPlayComplete", true ) );
  363. }
  364. }
  365.  
  366. // end video even if onPlayStatus does not fire
  367. if (myPlayheadTime == this.previousPlayheadTime)
  368. {
  369. if (! pause)
  370. {
  371. ++intBad;
  372. if (intBad > 8)
  373. {
  374. trace("intBad: pause = " + String( pause ) + "\n", "net" );
  375. stream.client.onPlayStatus();
  376. stream.close();
  377. this.dispatchEvent( new Event ( "videoPlayComplete", true ) );
  378. intBad = 0;
  379. }
  380. }
  381. }
  382. else
  383. {
  384. intBad = 0;
  385. }
  386. this.previousPlayheadTime = myPlayheadTime;
  387.  
  388. // check if passed cue point and if so fire event
  389. if (cuePoints)
  390. {
  391. for (i = 0; i < ASCuePoints.length; i++)
  392. {
  393. if (! ASCuePoints[i][2])
  394. {
  395. if (this.playheadTime() > ASCuePoints[i][0])
  396. {
  397. this.dispatchEvent( new Event ( ASCuePoints[ i ][ 1 ] + "CuePoint", true) );
  398. ASCuePoints[i][2] = true;
  399. }
  400. }
  401. }
  402. }
  403. }
  404.  
  405.  
  406. public function get paused():Boolean
  407. {
  408. return pause;
  409. }
  410. }
  411. }
  412.  
  413. /*
  414. import common.utils.Loggit;
  415.  
  416. public class CustomClient
  417. {
  418.  
  419. public var duration:Number;
  420. public var width:Number;
  421. public var height:Number;
  422. public var metaRec:Boolean;
  423. public var cuePoints:Array = new Array();
  424.  
  425.  
  426. public function onPlayStatus(info:Object = null ):void
  427. {
  428. trace("onPlayStatus: code=complete" + "\n", "net" );
  429. }
  430.  
  431.  
  432. public function onMetaData(info:Object):void
  433. {
  434. trace("metadata: duration=" + info.duration + " width=" + info.width + " height=" + info.height + " framerate=" + info.framerate + "\n", "net" );
  435. // assign returned meta data
  436. duration = info.duration;
  437. width = info.width;
  438. height = info.height;
  439. this.metaRec = true;
  440. }
  441.  
  442.  
  443. public function onCuePoint(info:Object):void
  444. {
  445. trace("cuepoint: time=" + info.time + " name=" + info.name + " type=" + info.type + "\n", "net" );
  446. cuePoints.push( [ info.time, info.name, info.type ] );
  447. }
  448. }
  449. */

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.