/src/away3d/materials/utils/SimpleVideoPlayer.as

https://github.com/ULuIQ12/away3d-core-fp11 · ActionScript · 338 lines · 245 code · 69 blank · 24 comment · 9 complexity · 3d3bea498af7cccc5e441bc69be3db6c MD5 · raw file

  1. package away3d.materials.utils
  2. {
  3. import flash.display.Sprite;
  4. import flash.events.AsyncErrorEvent;
  5. import flash.events.IOErrorEvent;
  6. import flash.events.NetStatusEvent;
  7. import flash.events.SecurityErrorEvent;
  8. import flash.media.SoundTransform;
  9. import flash.media.Video;
  10. import flash.net.NetConnection;
  11. import flash.net.NetStream;
  12. public class SimpleVideoPlayer implements IVideoPlayer
  13. {
  14. private var _src:String;
  15. private var _video:Video;
  16. private var _ns:NetStream;
  17. private var _nc:NetConnection;
  18. private var _nsClient:Object;
  19. private var _soundTransform:SoundTransform;
  20. private var _loop:Boolean;
  21. private var _playing:Boolean;
  22. private var _paused:Boolean;
  23. private var _lastVolume:Number;
  24. private var _container:Sprite;
  25. public function SimpleVideoPlayer()
  26. {
  27. // default values
  28. _soundTransform = new SoundTransform();
  29. _loop = false;
  30. _playing = false;
  31. _paused = false;
  32. _lastVolume = 1;
  33. // client object that'll redirect various calls from the video stream
  34. _nsClient = {};
  35. _nsClient["onCuePoint"] = metaDataHandler;
  36. _nsClient["onMetaData"] = metaDataHandler;
  37. _nsClient["onBWDone"] = onBWDone;
  38. _nsClient["close"] = streamClose;
  39. // NetConnection
  40. _nc = new NetConnection();
  41. _nc.client = _nsClient;
  42. _nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler, false, 0, true);
  43. _nc.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler, false, 0, true);
  44. _nc.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler, false, 0, true);
  45. _nc.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler, false, 0, true);
  46. _nc.connect(null);
  47. // NetStream
  48. _ns = new NetStream(_nc);
  49. _ns.checkPolicyFile = true;
  50. _ns.client = _nsClient;
  51. _ns.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler, false, 0, true);
  52. _ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler, false, 0, true);
  53. _ns.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler, false, 0, true);
  54. // video
  55. _video = new Video();
  56. _video.attachNetStream( _ns );
  57. // container
  58. _container = new Sprite();
  59. _container.addChild( _video );
  60. }
  61. //////////////////////////////////////////////////////
  62. // public methods
  63. //////////////////////////////////////////////////////
  64. public function play():void
  65. {
  66. if(!_src)
  67. {
  68. trace("Video source not set.");
  69. return;
  70. }
  71. if(_paused)
  72. {
  73. _ns.resume();
  74. _paused = false;
  75. _playing = true;
  76. }
  77. else if(!_playing)
  78. {
  79. _ns.play(_src);
  80. _playing = true;
  81. _paused = false;
  82. }
  83. }
  84. public function pause():void
  85. {
  86. if(!_paused)
  87. {
  88. _ns.pause();
  89. _paused = true;
  90. }
  91. }
  92. public function seek(val:Number):void
  93. {
  94. pause();
  95. _ns.seek( val );
  96. _ns.resume();
  97. }
  98. public function stop():void
  99. {
  100. _ns.close();
  101. _playing = false;
  102. _paused = false;
  103. }
  104. public function dispose():void
  105. {
  106. _ns.close();
  107. _video.attachNetStream( null );
  108. _ns.removeEventListener( NetStatusEvent.NET_STATUS, netStatusHandler );
  109. _ns.removeEventListener( AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler );
  110. _ns.removeEventListener( IOErrorEvent.IO_ERROR, ioErrorHandler );
  111. _nc.removeEventListener( NetStatusEvent.NET_STATUS, netStatusHandler );
  112. _nc.removeEventListener( SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler );
  113. _nc.removeEventListener( IOErrorEvent.IO_ERROR, ioErrorHandler );
  114. _nc.removeEventListener( AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler );
  115. _nsClient["onCuePoint"] = null;
  116. _nsClient["onMetaData"] = null;
  117. _nsClient["onBWDone"] = null;
  118. _nsClient["close"] = null;
  119. _container.removeChild( _video );
  120. _container = null;
  121. _src = null;
  122. _ns = null;
  123. _nc = null;
  124. _nsClient = null;
  125. _video = null;
  126. _soundTransform = null;
  127. _playing = false;
  128. _paused = false;
  129. }
  130. //////////////////////////////////////////////////////
  131. // event handlers
  132. //////////////////////////////////////////////////////
  133. private function asyncErrorHandler(event:AsyncErrorEvent): void
  134. {
  135. // Must be present to prevent errors, but won't do anything
  136. }
  137. private function metaDataHandler(oData:Object = null):void
  138. {
  139. // Offers info such as oData.duration, oData.width, oData.height, oData.framerate and more (if encoded into the FLV)
  140. //this.dispatchEvent( new VideoEvent(VideoEvent.METADATA,_netStream,file,oData) );
  141. }
  142. private function ioErrorHandler(e:IOErrorEvent):void
  143. {
  144. trace("An IOerror occured: "+e.text);
  145. }
  146. private function securityErrorHandler(e:SecurityErrorEvent):void
  147. {
  148. trace("A security error occured: "+e.text+" Remember that the FLV must be in the same security sandbox as your SWF.");
  149. }
  150. private function onBWDone():void
  151. {
  152. // Must be present to prevent errors for RTMP, but won't do anything
  153. }
  154. private function streamClose():void
  155. {
  156. trace("The stream was closed. Incorrect URL?");
  157. }
  158. private function netStatusHandler(e:NetStatusEvent):void
  159. {
  160. switch (e.info["code"]) {
  161. case "NetStream.Play.Stop":
  162. //this.dispatchEvent( new VideoEvent(VideoEvent.STOP,_netStream, file) );
  163. if(loop)
  164. _ns.play(_src);
  165. break;
  166. case "NetStream.Play.Play":
  167. //this.dispatchEvent( new VideoEvent(VideoEvent.PLAY,_netStream, file) );
  168. break;
  169. case "NetStream.Play.StreamNotFound":
  170. trace("The file "+ _src +" was not found", e);
  171. break;
  172. case "NetConnection.Connect.Success":
  173. trace("Connected to stream", e);
  174. break;
  175. }
  176. }
  177. //////////////////////////////////////////////////////
  178. // get / set functions
  179. //////////////////////////////////////////////////////
  180. public function get source():String
  181. {
  182. return _src;
  183. }
  184. public function set source(src:String):void
  185. {
  186. _src = src;
  187. if(_playing) _ns.play(_src);
  188. }
  189. public function get loop():Boolean
  190. {
  191. return _loop;
  192. }
  193. public function set loop(val:Boolean):void
  194. {
  195. _loop = val;
  196. }
  197. public function get volume():Number
  198. {
  199. return _ns.soundTransform.volume;
  200. }
  201. public function set volume(val:Number):void
  202. {
  203. _soundTransform.volume = val;
  204. _ns.soundTransform = _soundTransform;
  205. _lastVolume = val;
  206. }
  207. public function get pan():Number
  208. {
  209. return _ns.soundTransform.pan;
  210. }
  211. public function set pan(val:Number):void
  212. {
  213. _soundTransform.pan = pan;
  214. _ns.soundTransform = _soundTransform;
  215. }
  216. public function get mute():Boolean
  217. {
  218. return _ns.soundTransform.volume == 0;
  219. }
  220. public function set mute(val:Boolean):void
  221. {
  222. _soundTransform.volume = (val)? 0 : _lastVolume;
  223. _ns.soundTransform = _soundTransform;
  224. }
  225. public function get soundTransform():SoundTransform
  226. {
  227. return _ns.soundTransform;
  228. }
  229. public function set soundTransform(val:SoundTransform):void
  230. {
  231. _ns.soundTransform = val;
  232. }
  233. public function get width():int
  234. {
  235. return _video.width;
  236. }
  237. public function set width(val:int):void
  238. {
  239. _video.width = val;
  240. }
  241. public function get height():int
  242. {
  243. return _video.height;
  244. }
  245. public function set height(val:int):void
  246. {
  247. _video.height = val;
  248. }
  249. //////////////////////////////////////////////////////
  250. // read-only vars
  251. //////////////////////////////////////////////////////
  252. public function get container():Sprite
  253. {
  254. return _container;
  255. }
  256. public function get time():Number
  257. {
  258. return _ns.time;
  259. }
  260. public function get playing():Boolean
  261. {
  262. return _playing;
  263. }
  264. public function get paused():Boolean
  265. {
  266. return _paused;
  267. }
  268. }
  269. }