PageRenderTime 25ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/CLASSES/com/greensock.old/loading/data/MP3LoaderVars.as

https://github.com/slip/CourseBuilder
ActionScript | 209 lines | 90 code | 35 blank | 84 comment | 6 complexity | e08d5a6046b6ae39117a96f40702fa15 MD5 | raw file
  1. /**
  2. * VERSION: 1.1
  3. * DATE: 2010-12-09
  4. * AS3
  5. * UPDATES AND DOCS AT: http://www.greensock.com/loadermax/
  6. **/
  7. package com.greensock.loading.data {
  8. import flash.display.DisplayObject;
  9. import flash.media.SoundLoaderContext;
  10. /**
  11. * Can be used instead of a generic Object to define the <code>vars</code> parameter of a MP3Loader's constructor. <br /><br />
  12. *
  13. * There are 2 primary benefits of using a MP3LoaderVars instance to define your MP3Loader variables:
  14. * <ol>
  15. * <li> In most code editors, code hinting will be activated which helps remind you which special properties are available in MP3Loader</li>
  16. * <li> It enables strict data typing for improved debugging (ensuring, for example, that you don't define a Boolean value for <code>onComplete</code> where a Function is expected).</li>
  17. * </ol><br />
  18. *
  19. * The down side, of course, is that the code is more verbose and the MP3LoaderVars class adds slightly more kb to your swf.
  20. *
  21. * <b>USAGE:</b><br /><br />
  22. * Note that each method returns the MP3LoaderVars instance, so you can reduce the lines of code by method chaining (see example below).<br /><br />
  23. *
  24. * <b>Without MP3LoaderVars:</b><br /><code>
  25. * new MP3Loader("audio.mp3", {name:"audio", estimatedBytes:11500, autoPlay:false, onComplete:completeHandler, onProgress:progressHandler})</code><br /><br />
  26. *
  27. * <b>With MP3LoaderVars</b><br /><code>
  28. * new MP3Loader("audio.mp3", new MP3LoaderVars().name("audio").estimatedBytes(11500).autoPlay(false).onComplete(completeHandler).onProgress(progressHandler))</code><br /><br />
  29. *
  30. * <b>NOTES:</b><br />
  31. * <ul>
  32. * <li> To get the generic vars object that MP3LoaderVars builds internally, simply access its "vars" property.
  33. * In fact, if you want maximum backwards compatibility, you can tack ".vars" onto the end of your chain like this:<br /><code>
  34. * new MP3Loader("audio.mp3", new MP3LoaderVars().name("mp3").estimatedBytes(11500).autoPlay(false).vars);</code></li>
  35. * <li> Using MP3LoaderVars is completely optional. If you prefer the shorter synatax with the generic Object, feel
  36. * free to use it. The purpose of this class is simply to enable code hinting and to allow for strict data typing.</li>
  37. * </ul>
  38. *
  39. * <b>Copyright 2011, 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.
  40. *
  41. * @author Jack Doyle, jack@greensock.com
  42. **/
  43. public class MP3LoaderVars {
  44. /** @private **/
  45. public static const version:Number = 1.1;
  46. /** @private **/
  47. protected var _vars:Object;
  48. /**
  49. * Constructor
  50. * @param vars A generic Object containing properties that you'd like to add to this MP3LoaderVars instance.
  51. */
  52. public function MP3LoaderVars(vars:Object=null) {
  53. _vars = {};
  54. if (vars != null) {
  55. for (var p:String in vars) {
  56. _vars[p] = vars[p];
  57. }
  58. }
  59. }
  60. /** @private **/
  61. protected function _set(property:String, value:*):MP3LoaderVars {
  62. if (value == null) {
  63. delete _vars[property]; //in case it was previously set
  64. } else {
  65. _vars[property] = value;
  66. }
  67. return this;
  68. }
  69. /**
  70. * Adds a dynamic property to the vars object containing any value you want. This can be useful
  71. * in situations where you need to associate certain data with a particular loader. Just make sure
  72. * that the property name is a valid variable name (starts with a letter or underscore, no special characters, etc.)
  73. * and that it doesn't use a reserved property name like "name" or "onComplete", etc.
  74. *
  75. * For example, to set an "index" property to 5, do:
  76. *
  77. * <code>prop("index", 5);</code>
  78. *
  79. * @param property Property name
  80. * @param value Value
  81. */
  82. public function prop(property:String, value:*):MP3LoaderVars {
  83. return _set(property, value);
  84. }
  85. //---- LOADERCORE PROPERTIES -----------------------------------------------------------------
  86. /** 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>.**/
  87. public function autoDispose(value:Boolean):MP3LoaderVars {
  88. return _set("autoDispose", value);
  89. }
  90. /** A name that is used to identify the loader 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". **/
  91. public function name(value:String):MP3LoaderVars {
  92. return _set("name", value);
  93. }
  94. /** 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>). **/
  95. public function onCancel(value:Function):MP3LoaderVars {
  96. return _set("onCancel", value);
  97. }
  98. /** 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>). **/
  99. public function onComplete(value:Function):MP3LoaderVars {
  100. return _set("onComplete", value);
  101. }
  102. /** A handler function for <code>LoaderEvent.ERROR</code> events which are dispatched whenever the loader experiences an error (typically an IO_ERROR or SECURITY_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>). **/
  103. public function onError(value:Function):MP3LoaderVars {
  104. return _set("onError", value);
  105. }
  106. /** 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>). **/
  107. public function onFail(value:Function):MP3LoaderVars {
  108. return _set("onFail", value);
  109. }
  110. /** A handler function for <code>LoaderEvent.HTTP_STATUS</code> events. Make sure your onHTTPStatus function accepts a single parameter of type <code>LoaderEvent</code> (<code>com.greensock.events.LoaderEvent</code>). You can determine the httpStatus code using the LoaderEvent's <code>target.httpStatus</code> (LoaderItems keep track of their <code>httpStatus</code> when possible, although certain environments prevent Flash from getting httpStatus information).**/
  111. public function onHTTPStatus(value:Function):MP3LoaderVars {
  112. return _set("onHTTPStatus", value);
  113. }
  114. /** 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. public function onIOError(value:Function):MP3LoaderVars {
  116. return _set("onIOError", value);
  117. }
  118. /** 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>).**/
  119. public function onOpen(value:Function):MP3LoaderVars {
  120. return _set("onOpen", value);
  121. }
  122. /** 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>.**/
  123. public function onProgress(value:Function):MP3LoaderVars {
  124. return _set("onProgress", value);
  125. }
  126. /** LoaderMax supports <i>subloading</i>, where an object can be factored into a parent's loading progress. If you want LoaderMax to require this loader 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>vars.requireWithRoot = this.root;</code>. **/
  127. public function requireWithRoot(value:DisplayObject):MP3LoaderVars {
  128. return _set("requireWithRoot", value);
  129. }
  130. //---- LOADERITEM PROPERTIES -------------------------------------------------------------
  131. /** 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). **/
  132. public function alternateURL(value:String):MP3LoaderVars {
  133. return _set("alternateURL", value);
  134. }
  135. /** 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 is 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). **/
  136. public function estimatedBytes(value:uint):MP3LoaderVars {
  137. return _set("estimatedBytes", value);
  138. }
  139. /** If <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>LoaderMax.getLoader()</code> or <code>LoaderMax.getContent()</code> by <code>url</code> or when you're running locally). **/
  140. public function noCache(value:Boolean):MP3LoaderVars {
  141. return _set("noCache", value);
  142. }
  143. //---- MP3LOADER PROPERTIES --------------------------------------------------------------
  144. /** By default the MP3 will begin playing immediately when enough of the file has buffered, but to prevent it from autoPlaying, set <code>autoPlay</code> to <code>false</code>. **/
  145. public function autoPlay(value:Boolean):MP3LoaderVars {
  146. return _set("autoPlay", value);
  147. }
  148. /** Number of times that the mp3 should repeat. To repeat indefinitely, use -1. Default is 0. **/
  149. public function repeat(value:int):MP3LoaderVars {
  150. return _set("repeat", value);
  151. }
  152. /** A value between 0 and 1 indicating the volume at which the sound should play when the MP3Loader's controls are used to play the sound, like <code>playSound()</code> or when <code>autoPlay</code> is <code>true</code> (default volume is 1). **/
  153. public function volume(value:Number):MP3LoaderVars {
  154. return _set("volume", value);
  155. }
  156. /** To control things like the buffer time and whether or not a policy file is checked, define a <code>SoundLoaderContext</code> object. The default context is null. See Adobe's SoundLoaderContext documentation for details. **/
  157. public function context(value:SoundLoaderContext):MP3LoaderVars {
  158. return _set("context", value);
  159. }
  160. /** The minimum number of <code>bytesLoaded</code> to wait for before the <code>LoaderEvent.INIT</code> event is dispatched - the higher the number the more accurate the <code>duration</code> estimate will be when the INIT event is dispatched (the default value is 102400 which is 100k). The MP3's duration cannot be determined with 100% accuracy until it has completely loaded, but it is estimated with more and more accuracy as the file loads. **/
  161. public function initThreshold(value:uint):MP3LoaderVars {
  162. return _set("initThreshold", value);
  163. }
  164. //---- GETTERS / SETTERS -----------------------------------------------------------------
  165. /** The generic Object populated by all of the method calls in the MP3LoaderVars instance. This is the raw data that gets passed to the loader. **/
  166. public function get vars():Object {
  167. return _vars;
  168. }
  169. /** @private **/
  170. public function get isGSVars():Boolean {
  171. return true;
  172. }
  173. }
  174. }