Pyro Mediator for PureMVC


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

This is a PureMVC view mediator designed to interact with the Pyro video player from Turbulent Media. Must be used within the PureMVC Framework.


Copy this code and paste it in your HTML
  1. package com.acme.view
  2. {
  3. import flash.events.Event;
  4.  
  5. import org.puremvc.as3.interfaces.*;
  6. import org.puremvc.as3.patterns.mediator.Mediator;
  7.  
  8. import com.acme.ApplicationFacade;
  9.  
  10. import ca.turbulent.media.Pyro;
  11. import ca.turbulent.media.events.*
  12.  
  13. import flash.events.TimerEvent;
  14. import flash.utils.Timer;
  15. /**
  16.   * A Mediator for interacting with the Pyro Video Player.
  17.   */
  18. public class PyroMediator extends Mediator implements IMediator
  19. {
  20.  
  21. public static const NAME:String = 'PyroMediator';
  22. private var loadProgressTimer:Timer;
  23. private var playbackProgressTimer:Timer;
  24. private var loadProgress:Number;
  25. private var playbackProgress:Number;
  26. /**
  27.   * Constructor.
  28.   */
  29. public function PyroMediator( viewComponent:Object )
  30. {
  31. // pass the viewComponent to the superclass where
  32. // it will be stored in the inherited viewComponent property
  33.  
  34. super(NAME, viewComponent);
  35. loadProgressTimer=new Timer(500, 999);
  36. loadProgressTimer.addEventListener(TimerEvent.TIMER, checkLoadProgress);
  37. loadProgressTimer.addEventListener(TimerEvent.TIMER_COMPLETE, function(){loadProgressTimer.reset();loadProgressTimer.start();});
  38.  
  39. playbackProgressTimer=new Timer(500, 999);
  40. playbackProgressTimer.addEventListener(TimerEvent.TIMER, checkPlaybackProgress);
  41. playbackProgressTimer.addEventListener(TimerEvent.TIMER_COMPLETE, function(){playbackProgressTimer.reset();playbackProgressTimer.start();});
  42.  
  43.  
  44. myPyro.addEventListener( CuePointEvent.CUE_POINT_RECEIVED, onCuePointEvent );
  45. myPyro.addEventListener( ErrorEvent.CONNECTION_ERROR, onErrorEvent );
  46. myPyro.addEventListener( ErrorEvent.ERROR, onErrorEvent );
  47. myPyro.addEventListener( ErrorEvent.FILE_NOT_FOUND_ERROR, onErrorEvent );
  48. myPyro.addEventListener( ErrorEvent.SECURITY_ERROR, onErrorEvent );
  49. myPyro.addEventListener( ImageDataEvent.IMAGE_DATA_RECEIVED, onImageDateEvent );
  50. myPyro.addEventListener( PyroEvent.BANDWIDTH_CHECKED, onPyroEvent );
  51. myPyro.addEventListener( PyroEvent.BUFFER_EMPTY, onPyroEvent );
  52. myPyro.addEventListener( PyroEvent.BUFFER_FLUSH, onPyroEvent );
  53. myPyro.addEventListener( PyroEvent.BUFFER_FULL, onPyroEvent );
  54. myPyro.addEventListener( PyroEvent.BUFFER_TIME_ADJUSTED, onPyroEvent );
  55. myPyro.addEventListener( PyroEvent.CLOSE_CAPTIONS_UPDATE, onPyroEvent );
  56. myPyro.addEventListener( PyroEvent.COMPLETED, onPyroEvent );
  57. myPyro.addEventListener( PyroEvent.DISCONNECTED, onPyroEvent );
  58. myPyro.addEventListener( PyroEvent.INSUFFICIENT_BANDWIDTH, onPyroEvent );
  59. myPyro.addEventListener( PyroEvent.METADATA_RECEIVED, onPyroEvent );
  60. myPyro.addEventListener( PyroEvent.MUTED, onPyroEvent );
  61. myPyro.addEventListener( PyroEvent.NEW_STREAM_INIT, onPyroEvent );
  62. myPyro.addEventListener( PyroEvent.PAUSED, onPyroEvent );
  63. myPyro.addEventListener( PyroEvent.SEEKED, onPyroEvent );
  64. myPyro.addEventListener( PyroEvent.SIZE_UPDATE, onPyroEvent );
  65. myPyro.addEventListener( PyroEvent.STARTED, onPyroEvent );
  66. myPyro.addEventListener( PyroEvent.STOPPED, onPyroEvent );
  67. myPyro.addEventListener( PyroEvent.UNMUTED, onPyroEvent );
  68. myPyro.addEventListener( PyroEvent.UNPAUSED, onPyroEvent );
  69. myPyro.addEventListener( PyroEvent.URL_PARSED, onPyroEvent );
  70. myPyro.addEventListener( PyroEvent.VOLUME_UPDATE, onPyroEvent );
  71. myPyro.addEventListener( PyroEvent.XMP_DATA_RECEIVED, onPyroEvent );
  72. myPyro.addEventListener( StatusUpdateEvent.STATUS_UPDATE, onStatusUpdateEvent );
  73. myPyro.addEventListener( TextDataEvent.TEXT_DATA_RECEIVED, onTextDataEvent );
  74.  
  75. }
  76.  
  77.  
  78.  
  79. private function checkLoadProgress(e:Event):void{
  80. //loadProgress = myPyro.bytesLoaded / myPyro.bytesTotal;
  81. loadProgress = myPyro.loadRatio;;
  82. //playbackProgress = myPyro.progressRatio;
  83. //trace("loadProgress:"+loadProgress);
  84. sendNotification( ApplicationFacade.VIDEO_LOAD_PROGRESS,loadProgress );//goes to ControlsComponentMediator
  85.  
  86. if(loadProgress==1){
  87. loadProgressTimer.reset();
  88. }
  89. }
  90.  
  91.  
  92. private function checkPlaybackProgress(e:Event):void{
  93. playbackProgress = myPyro.progressRatio;
  94. //trace("checkPlaybackProgress:"+playbackProgress);
  95. sendNotification( ApplicationFacade.VIDEO_PLAYBACK_PROGRESS,playbackProgress );//goes to ControlsComponentMediator
  96. }
  97.  
  98.  
  99. private function onCuePointEvent(e:CuePointEvent):void{
  100. //trace("CuePointEvent:"+(CuePointEvent(e).cuePoint));
  101. }
  102. private function onErrorEvent(e:ErrorEvent):void{
  103. //trace("ErrorEvent:"+(ErrorEvent(e).errorMessage));
  104. }
  105. private function onImageDateEvent(e:ImageDataEvent):void{
  106. //trace("ImageDataEvent:"+(ImageDataEvent(e).image));
  107. }
  108. private function onPyroEvent(e:PyroEvent):void{
  109. //trace("PyroEvent:"+(PyroEvent(e).eventType));
  110. switch (PyroEvent(e).eventType){
  111. case "streamSeekedEvent":
  112. break;
  113. case "newStreamInitEvent":
  114. sendNotification( ApplicationFacade.VIDEO_STREAM_INIT );//indicates buffering has started, goes to controls component to show looper
  115. break;
  116. }
  117. }
  118. private function onStatusUpdateEvent(e:StatusUpdateEvent):void{
  119. trace("StatusUpdateEvent:"+(StatusUpdateEvent(e).status));
  120. switch (StatusUpdateEvent(e).status){
  121. case Pyro.STATUS_PLAYING:
  122. sendNotification( ApplicationFacade.VIDEO_IS_PLAYING );//goes to ControlsComponentMediator
  123. playbackProgressTimer.start();
  124. break;
  125. case Pyro.STATUS_READY:
  126. sendNotification( ApplicationFacade.VIDEO_PLAYER_IS_READY );
  127. break;
  128. case Pyro.STATUS_PENDING:
  129. loadProgressTimer.start();
  130. break;
  131. case Pyro.STATUS_CONNECTING:
  132. break;
  133. case Pyro.STATUS_PAUSED:
  134. playbackProgressTimer.stop();
  135. sendNotification( ApplicationFacade.VIDEO_IS_PAUSED );//goes to ControlsComponentMediator
  136. break;
  137. }
  138.  
  139. }
  140. private function onTextDataEvent(e:TextDataEvent):void{
  141. //trace("TextDataEvent:"+(TextDataEvent(e).text));
  142. }
  143. /**
  144.   * List all notifications this Mediator is interested in.
  145.   * <P>
  146.   * Automatically called by the framework when the mediator
  147.   * is registered with the view.</P>
  148.   *
  149.   * @return Array the list of Nofitication names
  150.   */
  151. override public function listNotificationInterests():Array
  152. {
  153. return [
  154. ApplicationFacade.PLAY_PAUSE,
  155. ApplicationFacade.VIDEO_PAUSE,
  156. ApplicationFacade.VIDEO_PLAY,
  157. ApplicationFacade.SEEK,
  158. ApplicationFacade.START_VIDEO_PLAYBACK,
  159. ApplicationFacade.VIDEO_SET_VOLUME
  160. ];
  161. }
  162.  
  163. /**
  164.   * Handle all notifications this Mediator is interested in.
  165.   * <P>
  166.   * Called by the framework when a notification is sent that
  167.   * this mediator expressed an interest in when registered
  168.   * (see <code>listNotificationInterests</code>.</P>
  169.   *
  170.   * @param INotification a notification
  171.   */
  172. override public function handleNotification( note:INotification ):void
  173. {
  174. switch ( note.getName() ) {
  175.  
  176. case ApplicationFacade.PLAY_PAUSE://comes from ControlsComponentMediator
  177. //trace("pyro playPause");
  178. myPyro.togglePause();
  179. break;
  180.  
  181. case ApplicationFacade.VIDEO_PAUSE://comes from ControlsComponentMediator
  182. trace("pyro pause");
  183. myPyro.pause();
  184. break;
  185.  
  186. case ApplicationFacade.VIDEO_PLAY://comes from ControlsComponentMediator
  187. trace("pyro play");
  188. myPyro.play();
  189. break;
  190.  
  191. case ApplicationFacade.SEEK: //comes from ControlsComponentMediator
  192. var t:uint = note.getBody() as uint;
  193. //trace("pyro seek:"+t);
  194. myPyro.seek(t);
  195. break;
  196.  
  197. case ApplicationFacade.START_VIDEO_PLAYBACK: //comes from ApplicationMediator
  198. var path:String = note.getBody() as String;
  199. myPyro.play(path);
  200. break;
  201.  
  202. case ApplicationFacade.VIDEO_SET_VOLUME:
  203. myPyro.volume=note.getBody() as Number;
  204. break;
  205. }
  206. }
  207.  
  208.  
  209. protected function get myPyro():Pyro
  210. {
  211. return viewComponent as Pyro;
  212. }
  213.  
  214. }
  215. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.