Posted By

useallfive on 12/02/08


Tagged

flash flv video as3


Versions (?)

Who likes this?

11 people have marked this snippet as a favorite

oamaxa
THEPWN3R
jpdevries
mattlohmann
Pingpongbal
motionmediasolutions
cpres
davidino1978
djmaddy
dubogii
malthouse


Streaming Video Player


 / Published in: ActionScript 3
 

URL: ua5-flashvideo

A great, simple way of streaming video

  1. package
  2. {
  3. import flash.events.*;
  4. import flash.media.Video;
  5. import flash.display.Sprite;
  6. import flash.net.*;
  7. import flash.text.TextField;
  8. import fl.controls.Button;
  9.  
  10. public class StreamingVideoPlayer extends Sprite {
  11. private var _videoURL:String;
  12. private var _video:Video;
  13. private var _vidDuration:Number;
  14. private var _vidXmax:Number;
  15. private var _vidYmax:Number;
  16. private var _vidWidth:Number;
  17. private var _vidHeight:Number;
  18. private var _main_nc:NetConnection = new NetConnection();
  19. private var _serverLoc:String;
  20. private var _ns:NetStream;
  21. private var _start_btn:Button;
  22.  
  23. /* ------------Contructors/Initialization-----------*/
  24. public function StreamingVideoPlayer(serverLoc:String,
  25. flvLocation:String,vidDuration:Number,vidXmax:Number,
  26. vidYmax:Number):void {
  27. //set video params
  28. _videoURL = flvLocation;
  29. _vidDuration = vidDuration;
  30. _vidXmax = vidXmax;
  31. _vidYmax = vidYmax;
  32. //add eventListeners to NetConnection and connect
  33. _main_nc.addEventListener(NetStatusEvent.NET_STATUS,
  34. onNetStatus);
  35. _main_nc.addEventListener(
  36. SecurityErrorEvent.SECURITY_ERROR,
  37. securityErrorHandler);
  38. _main_nc.connect(serverLoc);
  39. }
  40. private function onNetStatus(event:Object):void {
  41. //handles NetConnection and NetStream status events
  42. switch (event.info.code) {
  43. case "NetConnection.Connect.Success":
  44. //play stream if connection successful
  45. connectStream();
  46. break;
  47. case "NetStream.Play.StreamNotFound":
  48. //error if stream file not found in
  49. //location specified
  50. trace("Stream not found: " + _videoURL);
  51. break;
  52. case "NetStream.Play.Stop":
  53. //do if video is stopped
  54. videoPlayComplete();
  55. break;
  56. }
  57. //trace(event.info.code);
  58. }
  59.  
  60. /* -------------------Connection------------------- */
  61. private function connectStream():void {
  62. //netstream object
  63. _ns = new NetStream(_main_nc);
  64. _ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR,
  65. asyncErrorHandler);
  66. _ns.addEventListener(NetStatusEvent.NET_STATUS,
  67. onNetStatus);
  68. //other event handlers assigned
  69. //to the netstream client property
  70. var custom_obj:Object = new Object();
  71. custom_obj.onMetaData = onMetaDataHandler;
  72. custom_obj.onCuePoint = onCuePointHandler;
  73. custom_obj.onPlayStatus = playStatus;
  74. _ns.client = custom_obj;
  75. //attach netstream to the video object
  76. _video = new Video(_vidXmax,_vidYmax);
  77. _video.attachNetStream(_ns);
  78. //video position could be parameterized but hardcoded
  79. //here to account for optional movie frame border
  80. _video.x = 1;
  81. _video.y = 1;
  82. addChild(_video);
  83. setVideoInit();
  84. _ns.play(_videoURL);
  85. }
  86.  
  87. /* -------------NetStream actions and events-------------- */
  88. private function videoPlayComplete():void {
  89. setVideoInit();
  90. }
  91.  
  92. private function setVideoInit():void {
  93. _ns.play(_videoURL);
  94. _ns.pause();
  95. _ns.seek(_vidDuration/2);
  96. addStartBtn();
  97. }
  98. private function playStatus(event:Object):void {
  99. //handles onPlayStatus complete event if available
  100. switch (event.info.code) {
  101. case "NetStream.Play.Complete":
  102. //do if video play completes
  103. videoPlayComplete();
  104. break;
  105. }
  106. //trace(event.info.code);
  107. }
  108. private function playFlv(event:MouseEvent):void {
  109. _ns.play(_videoURL);
  110. //_ns.seek(192); //used for testing
  111. removeChild(_start_btn);
  112. }
  113. private function pauseFlv(event:MouseEvent):void {
  114. _ns.pause();
  115. }
  116.  
  117. /* ---------Buttons, labels, and other assets------------- */
  118. //could be placed in it's own VideoControler class
  119. private function addStartBtn():void {
  120. _start_btn = new Button();
  121. _start_btn.width = 80;
  122. _start_btn.x = (_vidXmax-_start_btn.width)/2+_video.x;
  123. _start_btn.y = (_vidYmax-_start_btn.height)/2+_video.y;
  124. _start_btn.label = "Start Video";
  125. _start_btn.addEventListener(MouseEvent.CLICK,playFlv);
  126. //addChild(_start_btn);
  127. }
  128.  
  129. /* -----------------Information handlers---------------- */
  130. private function onMetaDataHandler(metaInfoObj:Object):void {
  131. _video.width = metaInfoObj.width;
  132. _vidWidth = metaInfoObj.width;
  133. _video.height = metaInfoObj.height;
  134. _vidHeight = metaInfoObj.height;
  135. //trace("metadata: duration=" + metaInfoObj.duration +
  136. //"width=" + metaInfoObj.width + " height=" +
  137. //metaInfoObj.height + " framerate=" +
  138. //metaInfoObj.framerate);
  139. }
  140. private function onCuePointHandler(cueInfoObj:Object):void {
  141. //trace("cuepoint: time=" + cueInfoObj.time + " name=" +
  142. //cueInfoObj.name + " type=" + cueInfoObj.type);
  143. }
  144.  
  145. /* -----------------------Error handlers------------------------ */
  146. private function securityErrorHandler(event:SecurityErrorEvent):
  147. void {
  148. trace("securityErrorHandler: " + event);
  149. }
  150. private function asyncErrorHandler(event:AsyncErrorEvent):void {
  151. trace(event.text);
  152. }
  153. }
  154. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: malthouse on August 8, 2012

Nice markup. However video doesn't want to accept the _vidXmax and _vidYmax values for me. Any thoughts?

You need to login to post a comment.