PageRenderTime 68ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/cutload/com/greensock/loading/VideoLoader.as

http://cutload.googlecode.com/
ActionScript | 756 lines | 326 code | 40 blank | 390 comment | 99 complexity | f5243e29e53e3bfb0f055d4b819b4971 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. /**
  2. * VERSION: 1.8993
  3. * DATE: 2012-02-24
  4. * AS3
  5. * UPDATES AND DOCS AT: http://www.greensock.com/loadermax/
  6. **/
  7. package com.greensock.loading {
  8. import com.greensock.events.LoaderEvent;
  9. import com.greensock.loading.core.LoaderItem;
  10. import com.greensock.loading.display.ContentDisplay;
  11. import flash.display.Sprite;
  12. import flash.events.Event;
  13. import flash.events.NetStatusEvent;
  14. import flash.events.ProgressEvent;
  15. import flash.events.TimerEvent;
  16. import flash.media.SoundTransform;
  17. import flash.media.Video;
  18. import flash.net.NetConnection;
  19. import flash.net.NetStream;
  20. import flash.net.URLRequest;
  21. import flash.net.URLVariables;
  22. import flash.utils.Timer;
  23. import flash.utils.getTimer;
  24. /** Dispatched when the loader's <code>httpStatus</code> value changes. **/
  25. [Event(name="httpStatus", type="com.greensock.events.LoaderEvent")]
  26. /** Dispatched when the <code>netStream</code> dispatches a NET_STATUS event. **/
  27. [Event(name="netStatus", type="com.greensock.events.LoaderEvent")]
  28. /**
  29. * Loads an FLV, F4V, or MP4 video file using a NetStream and also provides convenient playback methods
  30. * and properties like <code>pauseVideo(), playVideo(), gotoVideoTime(), bufferProgress, playProgress, volume,
  31. * duration, videoPaused, metaData, </code> and <code>videoTime</code>. Just like ImageLoader and SWFLoader,
  32. * VideoLoader's <code>content</code> property refers to a <code>ContentDisplay</code> object (Sprite) that
  33. * gets created immediately so that you can position/scale/rotate it or add ROLL_OVER/ROLL_OUT/CLICK listeners
  34. * before (or while) the video loads. Use the VideoLoader's <code>content</code> property to get the ContentDisplay
  35. * Sprite, or use the <code>rawContent</code> property to get the <code>Video</code> object that is used inside the
  36. * ContentDisplay to display the video. If a <code>container</code> is defined in the <code>vars</code> object,
  37. * the ContentDisplay will immediately be added to that container).<br /><br />
  38. *
  39. * You don't need to worry about creating a NetConnection, a Video object, attaching the NetStream, or any
  40. * of the typical hassles. VideoLoader can even scale the video into the area you specify using scaleModes
  41. * like <code>"stretch", "proportionalInside", "proportionalOutside",</code> and more. A VideoLoader will
  42. * dispatch useful events like <code>VIDEO_COMPLETE, VIDEO_PAUSE, VIDEO_PLAY, VIDEO_BUFFER_FULL,
  43. * VIDEO_BUFFER_EMPTY, NET_STATUS, VIDEO_CUE_POINT</code>, and <code>PLAY_PROGRESS</code> in addition
  44. * to the typical loader events, making it easy to hook up your own control interface. It packs a
  45. * surprising amount of functionality into a very small amount of kb.<br /><br />
  46. *
  47. * <strong>OPTIONAL VARS PROPERTIES</strong><br />
  48. * The following special properties can be passed into the VideoLoader constructor via its <code>vars</code>
  49. * parameter which can be either a generic object or a <code><a href="data/VideoLoaderVars.html">VideoLoaderVars</a></code> object:<br />
  50. * <ul>
  51. * <li><strong> name : String</strong> - A name that is used to identify the VideoLoader instance. This name can be fed to the <code>LoaderMax.getLoader()</code> or <code>LoaderMax.getContent()</code> methods or traced at any time. Each loader's name should be unique. If you don't define one, a unique name will be created automatically, like "loader21".</li>
  52. * <li><strong> bufferTime : Number</strong> - The amount of time (in seconds) that should be buffered before the video can begin playing (set <code>autoPlay</code> to <code>false</code> to pause the video initially).</li>
  53. * <li><strong> autoPlay : Boolean</strong> - By default, the video will begin playing as soon as it has been adequately buffered, but to prevent it from playing initially, set <code>autoPlay</code> to <code>false</code>.</li>
  54. * <li><strong> smoothing : Boolean</strong> - When <code>smoothing</code> is <code>true</code> (the default), smoothing will be enabled for the video which typically leads to better scaling results.</li>
  55. * <li><strong> container : DisplayObjectContainer</strong> - A DisplayObjectContainer into which the <code>ContentDisplay</code> should be added immediately.</li>
  56. * <li><strong> width : Number</strong> - Sets the <code>ContentDisplay</code>'s <code>width</code> property (applied before rotation, scaleX, and scaleY).</li>
  57. * <li><strong> height : Number</strong> - Sets the <code>ContentDisplay</code>'s <code>height</code> property (applied before rotation, scaleX, and scaleY).</li>
  58. * <li><strong> centerRegistration : Boolean </strong> - if <code>true</code>, the registration point will be placed in the center of the <code>ContentDisplay</code> which can be useful if, for example, you want to animate its scale and have it grow/shrink from its center.</li>
  59. * <li><strong> scaleMode : String </strong> - When a <code>width</code> and <code>height</code> are defined, the <code>scaleMode</code> controls how the video will be scaled to fit the area. The following values are recognized (you may use the <code>com.greensock.layout.ScaleMode</code> constants if you prefer):
  60. * <ul>
  61. * <li><code>"stretch"</code> (the default) - The video will fill the width/height exactly.</li>
  62. * <li><code>"proportionalInside"</code> - The video will be scaled proportionally to fit inside the area defined by the width/height</li>
  63. * <li><code>"proportionalOutside"</code> - The video will be scaled proportionally to completely fill the area, allowing portions of it to exceed the bounds defined by the width/height.</li>
  64. * <li><code>"widthOnly"</code> - Only the width of the video will be adjusted to fit.</li>
  65. * <li><code>"heightOnly"</code> - Only the height of the video will be adjusted to fit.</li>
  66. * <li><code>"none"</code> - No scaling of the video will occur.</li>
  67. * </ul></li>
  68. * <li><strong> hAlign : String </strong> - When a <code>width</code> and <code>height</code> are defined, the <code>hAlign</code> determines how the video is horizontally aligned within that area. The following values are recognized (you may use the <code>com.greensock.layout.AlignMode</code> constants if you prefer):
  69. * <ul>
  70. * <li><code>"center"</code> (the default) - The video will be centered horizontally in the area</li>
  71. * <li><code>"left"</code> - The video will be aligned with the left side of the area</li>
  72. * <li><code>"right"</code> - The video will be aligned with the right side of the area</li>
  73. * </ul></li>
  74. * <li><strong> vAlign : String </strong> - When a <code>width</code> and <code>height</code> are defined, the <code>vAlign</code> determines how the video is vertically aligned within that area. The following values are recognized (you may use the <code>com.greensock.layout.AlignMode</code> constants if you prefer):
  75. * <ul>
  76. * <li><code>"center"</code> (the default) - The video will be centered vertically in the area</li>
  77. * <li><code>"top"</code> - The video will be aligned with the top of the area</li>
  78. * <li><code>"bottom"</code> - The video will be aligned with the bottom of the area</li>
  79. * </ul></li>
  80. * <li><strong> crop : Boolean</strong> - When a <code>width</code> and <code>height</code> are defined, setting <code>crop</code> to <code>true</code> will cause the video to be cropped within that area (by applying a <code>scrollRect</code> for maximum performance). This is typically useful when the <code>scaleMode</code> is <code>"proportionalOutside"</code> or <code>"none"</code> so that any parts of the video that exceed the dimensions defined by <code>width</code> and <code>height</code> are visually chopped off. Use the <code>hAlign</code> and <code>vAlign</code> special properties to control the vertical and horizontal alignment within the cropped area.</li>
  81. * <li><strong> x : Number</strong> - Sets the <code>ContentDisplay</code>'s <code>x</code> property (for positioning on the stage).</li>
  82. * <li><strong> y : Number</strong> - Sets the <code>ContentDisplay</code>'s <code>y</code> property (for positioning on the stage).</li>
  83. * <li><strong> scaleX : Number</strong> - Sets the <code>ContentDisplay</code>'s <code>scaleX</code> property.</li>
  84. * <li><strong> scaleY : Number</strong> - Sets the <code>ContentDisplay</code>'s <code>scaleY</code> property.</li>
  85. * <li><strong> rotation : Number</strong> - Sets the <code>ContentDisplay</code>'s <code>rotation</code> property.</li>
  86. * <li><strong> alpha : Number</strong> - Sets the <code>ContentDisplay</code>'s <code>alpha</code> property.</li>
  87. * <li><strong> visible : Boolean</strong> - Sets the <code>ContentDisplay</code>'s <code>visible</code> property.</li>
  88. * <li><strong> blendMode : String</strong> - Sets the <code>ContentDisplay</code>'s <code>blendMode</code> property.</li>
  89. * <li><strong> bgColor : uint </strong> - When a <code>width</code> and <code>height</code> are defined, a rectangle will be drawn inside the <code>ContentDisplay</code> immediately in order to ease the development process. It is transparent by default, but you may define a <code>bgAlpha</code> if you prefer.</li>
  90. * <li><strong> bgAlpha : Number </strong> - Controls the alpha of the rectangle that is drawn when a <code>width</code> and <code>height</code> are defined.</li>
  91. * <li><strong> volume : Number</strong> - A value between 0 and 1 indicating the volume at which the video should play (default is 1).</li>
  92. * <li><strong> repeat : int</strong> - Number of times that the video should repeat. To repeat indefinitely, use -1. Default is 0.</li>
  93. * <li><strong> checkPolicyFile : Boolean</strong> - If <code>true</code>, the VideoLoader will check for a crossdomain.xml file on the remote host (only useful when loading videos from other domains - see Adobe's docs for details about NetStream's <code>checkPolicyFile</code> property). </li>
  94. * <li><strong> estimatedDuration : Number</strong> - Estimated duration of the video in seconds. VideoLoader will only use this value until it receives the necessary metaData from the video in order to accurately determine the video's duration. You do not need to specify an <code>estimatedDuration</code>, but doing so can help make the playProgress and some other values more accurate (until the metaData has loaded). It can also make the <code>progress/bytesLoaded/bytesTotal</code> more accurate when a <code>estimatedDuration</code> is defined, particularly in <code>bufferMode</code>.</li>
  95. * <li><strong> deblocking : int</strong> - Indicates the type of filter applied to decoded video as part of post-processing. The default value is 0, which lets the video compressor apply a deblocking filter as needed. See Adobe's <code>flash.media.Video</code> class docs for details.</li>
  96. * <li><strong> bufferMode : Boolean </strong> - When <code>true</code>, the loader will report its progress only in terms of the video's buffer which can be very convenient if, for example, you want to display loading progress for the video's buffer or tuck it into a LoaderMax with other loaders and allow the LoaderMax to dispatch its <code>COMPLETE</code> event when the buffer is full instead of waiting for the whole file to download. When <code>bufferMode</code> is <code>true</code>, the VideoLoader will dispatch its <code>COMPLETE</code> event when the buffer is full as opposed to waiting for the entire video to load. You can toggle the <code>bufferMode</code> anytime. Please read the full <code>bufferMode</code> property ASDoc description below for details about how it affects things like <code>bytesTotal</code>.</li>
  97. * <li><strong> autoAdjustBuffer : Boolean </strong> If the buffer becomes empty during playback and <code>autoAdjustBuffer</code> is <code>true</code> (the default), it will automatically attempt to adjust the NetStream's <code>bufferTime</code> based on the rate at which the video has been loading, estimating what it needs to be in order to play the rest of the video without emptying the buffer again. This can prevent the annoying problem of video playback start/stopping/starting/stopping on a system tht doesn't have enough bandwidth to adequately buffer the video. You may also set the <code>bufferTime</code> in the constructor's <code>vars</code> parameter to set the initial value.</li>
  98. * <li><strong> autoDetachNetStream : Boolean</strong> - If <code>true</code>, the NetStream will only be attached to the Video object (the <code>rawContent</code>) when it is in the display list (on the stage). This conserves memory but it can cause a very brief rendering delay when the content is initially added to the stage (often imperceptible). Also, if you add it to the stage when the <code>videoTime</code> is <i>after</i> its last encoded keyframe, it will render at that last keyframe.</li>
  99. * <li><strong> alternateURL : String</strong> - If you define an <code>alternateURL</code>, the loader will initially try to load from its original <code>url</code> and if it fails, it will automatically (and permanently) change the loader's <code>url</code> to the <code>alternateURL</code> and try again. Think of it as a fallback or backup <code>url</code>. It is perfectly acceptable to use the same <code>alternateURL</code> for multiple loaders (maybe a default image for various ImageLoaders for example).</li>
  100. * <li><strong> noCache : Boolean</strong> - If <code>noCache</code> is <code>true</code>, a "gsCacheBusterID" parameter will be appended to the url with a random set of numbers to prevent caching (don't worry, this info is ignored when you <code>getLoader()</code> or <code>getContent()</code> by url and when you're running locally)</li>
  101. * <li><strong> estimatedBytes : uint</strong> - Initially, the loader's <code>bytesTotal</code> is set to the <code>estimatedBytes</code> value (or <code>LoaderMax.defaultEstimatedBytes</code> if one isn't defined). Then, when the loader begins loading and it can accurately determine the bytesTotal, it will do so. Setting <code>estimatedBytes</code> is optional, but the more accurate the value, the more accurate your loaders' overall progress will be initially. If the loader will be inserted into a LoaderMax instance (for queue management), its <code>auditSize</code> feature can attempt to automatically determine the <code>bytesTotal</code> at runtime (there is a slight performance penalty for this, however - see LoaderMax's documentation for details).</li>
  102. * <li><strong> requireWithRoot : DisplayObject</strong> - LoaderMax supports <i>subloading</i>, where an object can be factored into a parent's loading progress. If you want LoaderMax to require this VideoLoader as part of its parent SWFLoader's progress, you must set the <code>requireWithRoot</code> property to your swf's <code>root</code>. For example, <code>var loader:VideoLoader = new VideoLoader("myScript.php", {name:"textData", requireWithRoot:this.root});</code></li>
  103. * <li><strong> allowMalformedURL : Boolean</strong> - Normally, the URL will be parsed and any variables in the query string (like "?name=test&amp;state=il&amp;gender=m") will be placed into a URLVariables object which is added to the URLRequest. This avoids a few bugs in Flash, but if you need to keep the entire URL intact (no parsing into URLVariables), set <code>allowMalformedURL:true</code>. For example, if your URL has duplicate variables in the query string like <code>http://www.greensock.com/?c=S&amp;c=SE&amp;c=SW</code>, it is technically considered a malformed URL and a URLVariables object can't properly contain all the duplicates, so in this case you'd want to set <code>allowMalformedURL</code> to <code>true</code>.</li>
  104. * <li><strong> autoDispose : Boolean</strong> - When <code>autoDispose</code> is <code>true</code>, the loader will be disposed immediately after it completes (it calls the <code>dispose()</code> method internally after dispatching its <code>COMPLETE</code> event). This will remove any listeners that were defined in the vars object (like onComplete, onProgress, onError, onInit). Once a loader is disposed, it can no longer be found with <code>LoaderMax.getLoader()</code> or <code>LoaderMax.getContent()</code> - it is essentially destroyed but its content is not unloaded (you must call <code>unload()</code> or <code>dispose(true)</code> to unload its content). The default <code>autoDispose</code> value is <code>false</code>.
  105. *
  106. * <br /><br />----EVENT HANDLER SHORTCUTS----</li>
  107. * <li><strong> onOpen : Function</strong> - A handler function for <code>LoaderEvent.OPEN</code> events which are dispatched when the loader begins loading. Make sure your onOpen function accepts a single parameter of type <code>LoaderEvent</code> (<code>com.greensock.events.LoaderEvent</code>).</li>
  108. * <li><strong> onInit : Function</strong> - A handler function for <code>Event.INIT</code> events which will be called when the video's metaData has been received and the video is placed into the <code>ContentDisplay</code>. The <code>INIT</code> event can be dispatched more than once if the NetStream receives metaData more than once (which occasionally happens, particularly with F4V files - the first time often doesn't include the cuePoints). Make sure your <code>onInit</code> function accepts a single parameter of type <code>Event</code> (flash.events.Event).</li>
  109. * <li><strong> onProgress : Function</strong> - A handler function for <code>LoaderEvent.PROGRESS</code> events which are dispatched whenever the <code>bytesLoaded</code> changes. Make sure your onProgress function accepts a single parameter of type <code>LoaderEvent</code> (<code>com.greensock.events.LoaderEvent</code>). You can use the LoaderEvent's <code>target.progress</code> to get the loader's progress value or use its <code>target.bytesLoaded</code> and <code>target.bytesTotal</code>.</li>
  110. * <li><strong> onComplete : Function</strong> - A handler function for <code>LoaderEvent.COMPLETE</code> events which are dispatched when the loader has finished loading successfully. Make sure your onComplete function accepts a single parameter of type <code>LoaderEvent</code> (<code>com.greensock.events.LoaderEvent</code>).</li>
  111. * <li><strong> onCancel : Function</strong> - A handler function for <code>LoaderEvent.CANCEL</code> events which are dispatched when loading is aborted due to either a failure or because another loader was prioritized or <code>cancel()</code> was manually called. Make sure your onCancel function accepts a single parameter of type <code>LoaderEvent</code> (<code>com.greensock.events.LoaderEvent</code>).</li>
  112. * <li><strong> onError : Function</strong> - A handler function for <code>LoaderEvent.ERROR</code> events which are dispatched whenever the loader experiences an error (typically an IO_ERROR). An error doesn't necessarily mean the loader failed, however - to listen for when a loader fails, use the <code>onFail</code> special property. Make sure your onError function accepts a single parameter of type <code>LoaderEvent</code> (<code>com.greensock.events.LoaderEvent</code>).</li>
  113. * <li><strong> onFail : Function</strong> - A handler function for <code>LoaderEvent.FAIL</code> events which are dispatched whenever the loader fails and its <code>status</code> changes to <code>LoaderStatus.FAILED</code>. Make sure your onFail function accepts a single parameter of type <code>LoaderEvent</code> (<code>com.greensock.events.LoaderEvent</code>).</li>
  114. * <li><strong> onIOError : Function</strong> - A handler function for <code>LoaderEvent.IO_ERROR</code> events which will also call the onError handler, so you can use that as more of a catch-all whereas <code>onIOError</code> is specifically for LoaderEvent.IO_ERROR events. Make sure your onIOError function accepts a single parameter of type <code>LoaderEvent</code> (<code>com.greensock.events.LoaderEvent</code>).</li>
  115. * </ul><br />
  116. *
  117. * <strong>Note:</strong> Using a <code><a href="data/VideoLoaderVars.html">VideoLoaderVars</a></code> instance
  118. * instead of a generic object to define your <code>vars</code> is a bit more verbose but provides
  119. * code hinting and improved debugging because it enforces strict data typing. Use whichever one you prefer.<br /><br />
  120. *
  121. * <strong>Note:</strong> To avoid garbage collection issues in the Flash player, the <code>netStream</code>
  122. * object that VideoLoader employs must get recreated internally anytime the VideoLoader is unloaded or its loading
  123. * is cancelled, so if you need to directly access the <code>netStream</code>, it is best to do so <strong>after</strong>
  124. * the <code>COMPLETE</code> event has been dispatched. Otherwise, if you store a reference to the VideoLoader's
  125. * <code>netStream</code> before or during a load and it gets cancelled or unloaded for some reason, it won't reference
  126. * the one that was used to load the video.<br /><br />
  127. *
  128. * <strong>Note:</strong> There is a bug/inconsistency in Adobe's NetStream class that causes relative URLs
  129. * to use the swf's location as the base path instead of the HTML page's location like all other loaders. Therefore,
  130. * it would be wise to use the "base" attribute of the &lt;OBJECT&gt; and &lt;EMBED&gt; tags in the HTML to
  131. * make sure all relative paths are consistent. See <a href="http://kb2.adobe.com/cps/041/tn_04157.html" target="_blank">http://kb2.adobe.com/cps/041/tn_04157.html</a>
  132. * for details.<br /><br />
  133. *
  134. * <strong>Note:</strong> In order to minimize memory usage, VideoLoader doesn't attach the NetStream to its Video
  135. * object (the <code>rawContent</code>) until it is added to the display list. Therefore, if your VideoLoader's content
  136. * isn't somewhere on the stage, the NetStream's visual content won't be fully decoded into memory (that's a good thing).
  137. * The only time this could be of consequence is if you are trying to do a BitmapData.draw() of the VideoLoader's content
  138. * or rawContent when it isn't on the stage. In that case, you'd just need to attach the NetStream manually before doing
  139. * your BitmapData.draw() like <code>myVideoLoader.rawContent.attachNetStream(myVideoLoader.netStream)</code>. <br /><br />
  140. *
  141. * @example Example AS3 code:<listing version="3.0">
  142. import com.greensock.loading.~~;
  143. import com.greensock.loading.display.~~;
  144. import com.greensock.~~;
  145. import com.greensock.events.LoaderEvent;
  146. //create a VideoLoader
  147. var video:VideoLoader = new VideoLoader("assets/video.flv", {name:"myVideo", container:this, width:400, height:300, scaleMode:"proportionalInside", bgColor:0x000000, autoPlay:false, volume:0, requireWithRoot:this.root, estimatedBytes:75000});
  148. //start loading
  149. video.load();
  150. //add a CLICK listener to a button that causes the video to toggle its paused state.
  151. button.addEventListener(MouseEvent.CLICK, togglePause);
  152. function togglePause(event:MouseEvent):void {
  153. video.videoPaused = !video.videoPaused;
  154. }
  155. //or you could put the VideoLoader into a LoaderMax queue. Create one first...
  156. var queue:LoaderMax = new LoaderMax({name:"mainQueue", onProgress:progressHandler, onComplete:completeHandler, onError:errorHandler});
  157. //append the VideoLoader and several other loaders
  158. queue.append( video );
  159. queue.append( new DataLoader("assets/data.txt", {name:"myText"}) );
  160. queue.append( new ImageLoader("assets/image1.png", {name:"myImage", estimatedBytes:3500}) );
  161. //start loading the LoaderMax queue
  162. queue.load();
  163. function progressHandler(event:LoaderEvent):void {
  164. trace("progress: " + event.target.progress);
  165. }
  166. function completeHandler(event:LoaderEvent):void {
  167. //play the video
  168. video.playVideo();
  169. //tween the volume up to 1 over the course of 2 seconds.
  170. TweenLite.to(video, 2, {volume:1});
  171. }
  172. function errorHandler(event:LoaderEvent):void {
  173. trace("error occured with " + event.target + ": " + event.text);
  174. }
  175. </listing>
  176. *
  177. * <b>Copyright 2010-2012, GreenSock. All rights reserved.</b> This work is subject to the terms in <a href="http://www.greensock.com/terms_of_use.html">http://www.greensock.com/terms_of_use.html</a> or for corporate Club GreenSock members, the software agreement that was issued with the corporate membership.
  178. *
  179. * @see com.greensock.loading.data.VideoLoaderVars
  180. *
  181. * @author Jack Doyle, jack@greensock.com
  182. */
  183. public class VideoLoader extends LoaderItem {
  184. /** @private **/
  185. private static var _classActivated:Boolean = _activateClass("VideoLoader", VideoLoader, "flv,f4v,mp4,mov");
  186. /** Event type constant for when the video completes. **/
  187. public static const VIDEO_COMPLETE:String="videoComplete";
  188. /** Event type constant for when the video's buffer is full. **/
  189. public static const VIDEO_BUFFER_FULL:String="videoBufferFull";
  190. /** Event type constant for when the video's buffer is empty. **/
  191. public static const VIDEO_BUFFER_EMPTY:String="videoBufferEmpty";
  192. /** Event type constant for when the video is paused. **/
  193. public static const VIDEO_PAUSE:String="videoPause";
  194. /** Event type constant for when the video begins or resumes playing. If the buffer isn't full yet when VIDEO_PLAY is dispatched, the video will wait to visually begin playing until the buffer is full. So VIDEO_PLAY indicates when the NetStream received an instruction to play, not necessarily when it visually begins playing. **/
  195. public static const VIDEO_PLAY:String="videoPlay";
  196. /** Event type constant for when the video reaches a cue point in the playback of the NetStream. **/
  197. public static const VIDEO_CUE_POINT:String="videoCuePoint";
  198. /** Event type constant for when the playback progresses (only dispatched when the video is playing). **/
  199. public static const PLAY_PROGRESS:String="playProgress";
  200. /** @private **/
  201. protected var _ns:NetStream;
  202. /** @private **/
  203. protected var _nc:NetConnection;
  204. /** @private **/
  205. protected var _auditNS:NetStream;
  206. /** @private **/
  207. protected var _video:Video;
  208. /** @private **/
  209. protected var _sound:SoundTransform;
  210. /** @private **/
  211. protected var _videoPaused:Boolean;
  212. /** @private **/
  213. protected var _videoComplete:Boolean;
  214. /** @private **/
  215. protected var _forceTime:Number;
  216. /** @private **/
  217. protected var _duration:Number;
  218. /** @private **/
  219. protected var _pausePending:Boolean;
  220. /** @private **/
  221. protected var _volume:Number;
  222. /** @private **/
  223. protected var _sprite:Sprite;
  224. /** @private **/
  225. protected var _initted:Boolean;
  226. /** @private **/
  227. protected var _bufferMode:Boolean;
  228. /** @private **/
  229. protected var _repeatCount:uint;
  230. /** @private **/
  231. protected var _bufferFull:Boolean;
  232. /** @private **/
  233. protected var _dispatchPlayProgress:Boolean;
  234. /** @private **/
  235. protected var _prevTime:Number;
  236. /** @private **/
  237. protected var _prevCueTime:Number;
  238. /** @private **/
  239. protected var _firstCuePoint:CuePoint;
  240. /** @private due to a bug in the NetStream class, we cannot seek() or pause() before the NetStream has dispatched a RENDER Event (or after 50ms for Flash Player 9). **/
  241. protected var _renderedOnce:Boolean;
  242. /** @private primarily used for FP9 to work around a Flash bug with seek() and pause() (see the _waitForRender() method for note). **/
  243. protected var _renderTimer:Timer;
  244. /** @private **/
  245. protected var _autoDetachNetStream:Boolean;
  246. /** @private the first VIDEO_PLAY event shouldn't be dispatched until the NetStream's NetStatusEvent fires with the code NetStream.Play.Start gets dispatched, so we track it with this Boolean variable. Otherwise, if you create a VideoLoader with autoPlay:false and then immediately load() and playVideo(), it would dispatch the VIDEO_PLAY event twice, once for the playVideo() and once when the NetStatusEvent is received. **/
  247. protected var _playStarted:Boolean;
  248. /** The metaData that was received from the video (contains information about its width, height, frame rate, etc.). See Adobe's docs for information about a NetStream's onMetaData callback. **/
  249. public var metaData:Object;
  250. /** If the buffer becomes empty during playback and <code>autoAdjustBuffer</code> is <code>true</code> (the default), it will automatically attempt to adjust the NetStream's <code>bufferTime</code> based on the rate at which the video has been loading, estimating what it needs to be in order to play the rest of the video without emptying the buffer again. This can prevent the annoying problem of video playback start/stopping/starting/stopping on a system tht doesn't have enough bandwidth to adequately buffer the video. You may also set the <code>bufferTime</code> in the constructor's <code>vars</code> parameter to set the initial value. **/
  251. public var autoAdjustBuffer:Boolean;
  252. /**
  253. * Constructor
  254. *
  255. * @param urlOrRequest The url (<code>String</code>) or <code>URLRequest</code> from which the loader should get its content.
  256. * @param vars An object containing optional configuration details. For example: <code>new VideoLoader("video/video.flv", {name:"myVideo", onComplete:completeHandler, onProgress:progressHandler})</code>.<br /><br />
  257. *
  258. * The following special properties can be passed into the constructor via the <code>vars</code> parameter
  259. * which can be either a generic object or a <code><a href="data/VideoLoaderVars.html">VideoLoaderVars</a></code> object:<br />
  260. * <ul>
  261. * <li><strong> name : String</strong> - A name that is used to identify the VideoLoader instance. This name can be fed to the <code>LoaderMax.getLoader()</code> or <code>LoaderMax.getContent()</code> methods or traced at any time. Each loader's name should be unique. If you don't define one, a unique name will be created automatically, like "loader21".</li>
  262. * <li><strong> bufferTime : Number</strong> - The amount of time (in seconds) that should be buffered before the video can begin playing (set <code>autoPlay</code> to <code>false</code> to pause the video initially).</li>
  263. * <li><strong> autoPlay : Boolean</strong> - By default, the video will begin playing as soon as it has been adequately buffered, but to prevent it from playing initially, set <code>autoPlay</code> to <code>false</code>.</li>
  264. * <li><strong> smoothing : Boolean</strong> - When <code>smoothing</code> is <code>true</code> (the default), smoothing will be enabled for the video which typically leads to better scaling results.</li>
  265. * <li><strong> container : DisplayObjectContainer</strong> - A DisplayObjectContainer into which the <code>ContentDisplay</code> should be added immediately.</li>
  266. * <li><strong> width : Number</strong> - Sets the <code>ContentDisplay</code>'s <code>width</code> property (applied before rotation, scaleX, and scaleY).</li>
  267. * <li><strong> height : Number</strong> - Sets the <code>ContentDisplay</code>'s <code>height</code> property (applied before rotation, scaleX, and scaleY).</li>
  268. * <li><strong> centerRegistration : Boolean </strong> - if <code>true</code>, the registration point will be placed in the center of the <code>ContentDisplay</code> which can be useful if, for example, you want to animate its scale and have it grow/shrink from its center.</li>
  269. * <li><strong> scaleMode : String </strong> - When a <code>width</code> and <code>height</code> are defined, the <code>scaleMode</code> controls how the video will be scaled to fit the area. The following values are recognized (you may use the <code>com.greensock.layout.ScaleMode</code> constants if you prefer):
  270. * <ul>
  271. * <li><code>"stretch"</code> (the default) - The video will fill the width/height exactly.</li>
  272. * <li><code>"proportionalInside"</code> - The video will be scaled proportionally to fit inside the area defined by the width/height</li>
  273. * <li><code>"proportionalOutside"</code> - The video will be scaled proportionally to completely fill the area, allowing portions of it to exceed the bounds defined by the width/height.</li>
  274. * <li><code>"widthOnly"</code> - Only the width of the video will be adjusted to fit.</li>
  275. * <li><code>"heightOnly"</code> - Only the height of the video will be adjusted to fit.</li>
  276. * <li><code>"none"</code> - No scaling of the video will occur.</li>
  277. * </ul></li>
  278. * <li><strong> hAlign : String </strong> - When a <code>width</code> and <code>height</code> are defined, the <code>hAlign</code> determines how the video is horizontally aligned within that area. The following values are recognized (you may use the <code>com.greensock.layout.AlignMode</code> constants if you prefer):
  279. * <ul>
  280. * <li><code>"center"</code> (the default) - The video will be centered horizontally in the area</li>
  281. * <li><code>"left"</code> - The video will be aligned with the left side of the area</li>
  282. * <li><code>"right"</code> - The video will be aligned with the right side of the area</li>
  283. * </ul></li>
  284. * <li><strong> vAlign : String </strong> - When a <code>width</code> and <code>height</code> are defined, the <code>vAlign</code> determines how the video is vertically aligned within that area. The following values are recognized (you may use the <code>com.greensock.layout.AlignMode</code> constants if you prefer):
  285. * <ul>
  286. * <li><code>"center"</code> (the default) - The video will be centered vertically in the area</li>
  287. * <li><code>"top"</code> - The video will be aligned with the top of the area</li>
  288. * <li><code>"bottom"</code> - The video will be aligned with the bottom of the area</li>
  289. * </ul></li>
  290. * <li><strong> crop : Boolean</strong> - When a <code>width</code> and <code>height</code> are defined, setting <code>crop</code> to <code>true</code> will cause the video to be cropped within that area (by applying a <code>scrollRect</code> for maximum performance). This is typically useful when the <code>scaleMode</code> is <code>"proportionalOutside"</code> or <code>"none"</code> so that any parts of the video that exceed the dimensions defined by <code>width</code> and <code>height</code> are visually chopped off. Use the <code>hAlign</code> and <code>vAlign</code> special properties to control the vertical and horizontal alignment within the cropped area.</li>
  291. * <li><strong> x : Number</strong> - Sets the <code>ContentDisplay</code>'s <code>x</code> property (for positioning on the stage).</li>
  292. * <li><strong> y : Number</strong> - Sets the <code>ContentDisplay</code>'s <code>y</code> property (for positioning on the stage).</li>
  293. * <li><strong> scaleX : Number</strong> - Sets the <code>ContentDisplay</code>'s <code>scaleX</code> property.</li>
  294. * <li><strong> scaleY : Number</strong> - Sets the <code>ContentDisplay</code>'s <code>scaleY</code> property.</li>
  295. * <li><strong> rotation : Number</strong> - Sets the <code>ContentDisplay</code>'s <code>rotation</code> property.</li>
  296. * <li><strong> alpha : Number</strong> - Sets the <code>ContentDisplay</code>'s <code>alpha</code> property.</li>
  297. * <li><strong> visible : Boolean</strong> - Sets the <code>ContentDisplay</code>'s <code>visible</code> property.</li>
  298. * <li><strong> blendMode : String</strong> - Sets the <code>ContentDisplay</code>'s <code>blendMode</code> property.</li>
  299. * <li><strong> bgColor : uint </strong> - When a <code>width</code> and <code>height</code> are defined, a rectangle will be drawn inside the <code>ContentDisplay</code> immediately in order to ease the development process. It is transparent by default, but you may define a <code>bgAlpha</code> if you prefer.</li>
  300. * <li><strong> bgAlpha : Number </strong> - Controls the alpha of the rectangle that is drawn when a <code>width</code> and <code>height</code> are defined.</li>
  301. * <li><strong> volume : Number</strong> - A value between 0 and 1 indicating the volume at which the video should play (default is 1).</li>
  302. * <li><strong> repeat : int</strong> - Number of times that the video should repeat. To repeat indefinitely, use -1. Default is 0.</li>
  303. * <li><strong> checkPolicyFile : Boolean</strong> - If <code>true</code>, the VideoLoader will check for a crossdomain.xml file on the remote host (only useful when loading videos from other domains - see Adobe's docs for details about NetStream's <code>checkPolicyFile</code> property). </li>
  304. * <li><strong> estimatedDuration : Number</strong> - Estimated duration of the video in seconds. VideoLoader will only use this value until it receives the necessary metaData from the video in order to accurately determine the video's duration. You do not need to specify an <code>estimatedDuration</code>, but doing so can help make the playProgress and some other values more accurate (until the metaData has loaded). It can also make the <code>progress/bytesLoaded/bytesTotal</code> more accurate when a <code>estimatedDuration</code> is defined, particularly in <code>bufferMode</code>.</li>
  305. * <li><strong> deblocking : int</strong> - Indicates the type of filter applied to decoded video as part of post-processing. The default value is 0, which lets the video compressor apply a deblocking filter as needed. See Adobe's <code>flash.media.Video</code> class docs for details.</li>
  306. * <li><strong> bufferMode : Boolean </strong> - When <code>true</code>, the loader will report its progress only in terms of the video's buffer which can be very convenient if, for example, you want to display loading progress for the video's buffer or tuck it into a LoaderMax with other loaders and allow the LoaderMax to dispatch its <code>COMPLETE</code> event when the buffer is full instead of waiting for the whole file to download. When <code>bufferMode</code> is <code>true</code>, the VideoLoader will dispatch its <code>COMPLETE</code> event when the buffer is full as opposed to waiting for the entire video to load. You can toggle the <code>bufferMode</code> anytime. Please read the full <code>bufferMode</code> property ASDoc description below for details about how it affects things like <code>bytesTotal</code>.</li>
  307. * <li><strong> autoAdjustBuffer : Boolean </strong> If the buffer becomes empty during playback and <code>autoAdjustBuffer</code> is <code>true</code> (the default), it will automatically attempt to adjust the NetStream's <code>bufferTime</code> based on the rate at which the video has been loading, estimating what it needs to be in order to play the rest of the video without emptying the buffer again. This can prevent the annoying problem of video playback start/stopping/starting/stopping on a system tht doesn't have enough bandwidth to adequately buffer the video. You may also set the <code>bufferTime</code> in the constructor's <code>vars</code> parameter to set the initial value.</li>
  308. * <li><strong> autoDetachNetStream : Boolean</strong> - If <code>true</code>, the NetStream will only be attached to the Video object (the <code>rawContent</code>) when it is in the display list (on the stage). This conserves memory but it can cause a very brief rendering delay when the content is initially added to the stage (often imperceptible). Also, if you add it to the stage when the <code>videoTime</code> is <i>after</i> its last encoded keyframe, it will render at that last keyframe.</li>
  309. * <li><strong> alternateURL : String</strong> - If you define an <code>alternateURL</code>, the loader will initially try to load from its original <code>url</code> and if it fails, it will automatically (and permanently) change the loader's <code>url</code> to the <code>alternateURL</code> and try again. Think of it as a fallback or backup <code>url</code>. It is perfectly acceptable to use the same <code>alternateURL</code> for multiple loaders (maybe a default image for various ImageLoaders for example).</li>
  310. * <li><strong> noCache : Boolean</strong> - If <code>noCache</code> is <code>true</code>, a "gsCacheBusterID" parameter will be appended to the url with a random set of numbers to prevent caching (don't worry, this info is ignored when you <code>getLoader()</code> or <code>getContent()</code> by url and when you're running locally)</li>
  311. * <li><strong> estimatedBytes : uint</strong> - Initially, the loader's <code>bytesTotal</code> is set to the <code>estimatedBytes</code> value (or <code>LoaderMax.defaultEstimatedBytes</code> if one isn't defined). Then, when the loader begins loading and it can accurately determine the bytesTotal, it will do so. Setting <code>estimatedBytes</code> is optional, but the more accurate the value, the more accurate your loaders' overall progress will be initially. If the loader will be inserted into a LoaderMax instance (for queue management), its <code>auditSize</code> feature can attempt to automatically determine the <code>bytesTotal</code> at runtime (there is a slight performance penalty for this, however - see LoaderMax's documentation for details).</li>
  312. * <li><strong> requireWithRoot : DisplayObject</strong> - LoaderMax supports <i>subloading</i>, where an object can be factored into a parent's loading progress. If you want LoaderMax to require this VideoLoader as part of its parent SWFLoader's progress, you must set the <code>requireWithRoot</code> property to your swf's <code>root</code>. For example, <code>var loader:VideoLoader = new VideoLoader("myScript.php", {name:"textData", requireWithRoot:this.root});</code></li>
  313. * <li><strong> allowMalformedURL : Boolean</strong> - Normally, the URL will be parsed and any variables in the query string (like "?name=test&amp;state=il&amp;gender=m") will be placed into a URLVariables object which is added to the URLRequest. This avoids a few bugs in Flash, but if you need to keep the entire URL intact (no parsing into URLVariables), set <code>allowMalformedURL:true</code>. For example, if your URL has duplicate variables in the query string like <code>http://www.greensock.com/?c=S&amp;c=SE&amp;c=SW</code>, it is technically considered a malformed URL and a URLVariables object can't properly contain all the duplicates, so in this case you'd want to set <code>allowMalformedURL</code> to <code>true</code>.</li>
  314. * <li><strong> autoDispose : Boolean</strong> - When <code>autoDispose</code> is <code>true</code>, the loader will be disposed immediately after it completes (it calls the <code>dispose()</code> method internally after dispatching its <code>COMPLETE</code> event). This will remove any listeners that were defined in the vars object (like onComplete, onProgress, onError, onInit). Once a loader is disposed, it can no longer be found with <code>LoaderMax.getLoader()</code> or <code>LoaderMax.getContent()</code> - it is essentially destroyed but its content is not unloaded (you must call <code>unload()</code> or <code>dispose(true)</code> to unload its content). The default <code>autoDispose</code> value is <code>false</code>.
  315. *
  316. * <br /><br />----EVENT HANDLER SHORTCUTS----</li>
  317. * <li><strong> onOpen : Function</strong> - A handler function for <code>LoaderEvent.OPEN</code> events which are dispatched when the loader begins loading. Make sure your onOpen function accepts a single parameter of type <code>LoaderEvent</code> (<code>com.greensock.events.LoaderEvent</code>).</li>
  318. * <li><strong> onInit : Function</strong> - A handler function for <code>Event.INIT</code> events which will be called when the video's metaData has been received and the video is placed into the <code>ContentDisplay</code>. The <code>INIT</code> event can be dispatched more than once if the NetStream receives metaData more than once (which occasionally happens, particularly with F4V files - the first time often doesn't include the cuePoints). Make sure your <code>onInit</code> function accepts a single parameter of type <code>Event</code> (flash.events.Event).</li>
  319. * <li><strong> onProgress : Function</strong> - A handler function for <code>LoaderEvent.PROGRESS</code> events which are dispatched whenever the <code>bytesLoaded</code> changes. Make sure your onProgress function accepts a single parameter of type <code>LoaderEvent</code> (<code>com.greensock.events.LoaderEvent</code>). You can use the LoaderEvent's <code>target.progress</code> to get the loader's progress value or use its <code>target.bytesLoaded</code> and <code>target.bytesTotal</code>.</li>
  320. * <li><strong> onComplete : Function</strong> - A handler function for <code>LoaderEvent.COMPLETE</code> events which are dispatched when the loader has finished loading successfully. Make sure your onComplete function accepts a single parameter of type <code>LoaderEvent</code> (<code>com.greensock.events.LoaderEvent</code>).</li>
  321. * <li><strong> onCancel : Function</strong> - A handler function for <code>LoaderEvent.CANCEL</code> events which are dispatched when loading is aborted due to either a failure or because another loader was prioritized or <code>cancel()</code> was manually called. Make sure your onCancel function accepts a single parameter of type <code>LoaderEvent</code> (<code>com.greensock.events.LoaderEvent</code>).</li>
  322. * <li><strong> onError : Function</strong> - A handler function for <code>LoaderEvent.ERROR</code> events which are dispatched whenever the loader experiences an error (typically an IO_ERROR). An error doesn't necessarily mean the loader failed, however - to listen for when a loader fails, use the <code>onFail</code> special property. Make sure your onError function accepts a single parameter of type <code>LoaderEvent</code> (<code>com.greensock.events.LoaderEvent</code>).</li>
  323. * <li><strong> onFail : Function</strong> - A handler function for <code>LoaderEvent.FAIL</code> events which are dispatched whenever the loader fails and its <code>status</code> changes to <code>LoaderStatus.FAILED</code>. Make sure your onFail function accepts a single parameter of type <code>LoaderEvent</code> (<code>com.greensock.events.LoaderEvent</code>).</li>
  324. * <li><strong> onIOError : Function</strong> - A handler function for <code>LoaderEvent.IO_ERROR</code> events which will also call the onError handler, so you can use that as more of a catch-all whereas <code>onIOError</code> is specifically for LoaderEvent.IO_ERROR events. Make sure your onIOError function accepts a single parameter of type <code>LoaderEvent</code> (<code>com.greensock.events.LoaderEvent</code>).</li>
  325. * </ul>
  326. * @see com.greensock.loading.data.VideoLoaderVars
  327. */
  328. public function VideoLoader(urlOrRequest:*, vars:Object=null) {
  329. super(urlOrRequest, vars);
  330. _type = "VideoLoader";
  331. _nc = new NetConnection();
  332. _nc.connect(null);
  333. _nc.addEventListener("asyncError", _failHandler, false, 0, true);
  334. _nc.addEventListener("securityError", _failHandler, false, 0, true);
  335. _renderTimer = new Timer(80, 0);
  336. _renderTimer.addEventListener(TimerEvent.TIMER, _renderHandler, false, 0, true);
  337. _video = new Video(this.vars.width || 320, this.vars.height || 240);
  338. _video.smoothing = Boolean(this.vars.smoothing != false);
  339. _video.deblocking = uint(this.vars.deblocking);
  340. //the video isn't decoded into memory fully until the NetStream is attached to the Video object. We only attach it when it is in the display list (thus can be seen) in order to conserve memory.
  341. _video.addEventListener(Event.ADDED_TO_STAGE, _videoAddedToStage, false, 0, true);
  342. _video.addEventListener(Event.REMOVED_FROM_STAGE, _videoRemovedFromStage, false, 0, true);
  343. _autoDetachNetStream = Boolean(this.vars.autoDetachNetStream == true);
  344. _refreshNetStream();
  345. _duration = isNaN(this.vars.estimatedDuration) ? 200 : Number(this.vars.estimatedDuration); //just set it to a high number so that the progress starts out low.
  346. _bufferMode = _preferEstimatedBytesInAudit = Boolean(this.vars.bufferMode == true);
  347. _videoPaused = _pausePending = Boolean(this.vars.autoPlay == false);
  348. this.autoAdjustBuffer = !(this.vars.autoAdjustBuffer == false);
  349. this.volume = ("volume" in this.vars) ? Number(this.vars.volume) : 1;
  350. if (LoaderMax.contentDisplayClass is Class) {
  351. _sprite = new LoaderMax.contentDisplayClass(this);
  352. if (!_sprite.hasOwnProperty("rawContent")) {
  353. throw new Error("LoaderMax.contentDisplayClass must be set to a class with a 'rawContent' property, like com.greensock.loading.display.ContentDisplay");
  354. }
  355. } else {
  356. _sprite = new ContentDisplay(this);
  357. }
  358. Object(_sprite).rawContent = null; //so that the video doesn't initially show at the wrong size before the metaData is received at which point we can accurately determine the aspect ratio.
  359. }
  360. /** @private **/
  361. protected function _refreshNetStream():void {
  362. if (_ns != null) {
  363. _ns.pause();
  364. try {
  365. _ns.close();
  366. } catch (error:Error) {
  367. }
  368. _sprite.removeEventListener(Event.ENTER_FRAME, _playProgressHandler);
  369. _video.attachNetStream(null);
  370. _video.clear();
  371. _ns.client = {};
  372. _ns.removeEventListener(NetStatusEvent.NET_STATUS, _statusHandler);
  373. _ns.removeEventListener("ioError", _failHandler);
  374. _ns.removeEventListener("asyncError", _failHandler);
  375. _ns.removeEventListener(Event.RENDER, _renderHandler);
  376. }
  377. _prevTime = _prevCueTime = 0;
  378. _ns = (this.vars.netStream is NetStream) ? this.vars.netStream : new NetStream(_nc);
  379. _ns.checkPolicyFile = Boolean(this.vars.checkPolicyFile == true);
  380. _ns.client = {onMetaData:_metaDataHandler, onCuePoint:_cuePointHandler};
  381. _ns.addEventListener(NetStatusEvent.NET_STATUS, _statusHandler, false, 0, true);
  382. _ns.addEventListener("ioError", _failHandler, false, 0, true);
  383. _ns.addEventListener("asyncError", _failHandler, false, 0, true);
  384. _ns.bufferTime = isNaN(this.vars.bufferTime) ? 5 : Number

Large files files are truncated, but you can click here to view the full file