PageRenderTime 24ms CodeModel.GetById 18ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/src/org/ishafoundation/archives/transcript/media/MediaConnection.as

http://transcriptstudio4isha.googlecode.com/
ActionScript | 118 lines | 98 code | 10 blank | 10 comment | 22 complexity | ed914c792d28e4aa2cda8318df7cdc58 MD5 | raw file
  1package org.ishafoundation.archives.transcript.media
  2{
  3	import flash.events.NetStatusEvent;
  4	import flash.events.SecurityErrorEvent;
  5	import flash.media.Video;
  6	import flash.net.NetConnection;
  7	
  8	import org.ishafoundation.archives.transcript.util.PreferencesSharedObject;
  9	
 10	public class MediaConnection
 11	{
 12		internal var _nc:NetConnection;	
 13		[Bindable]	
 14		public var mediaStream:MediaStream;
 15		
 16		/** connection in progress? */
 17		private var connecting:Boolean = false;
 18		
 19		public function MediaConnection()
 20		{
 21		}
 22		
 23		/**
 24		 * Don't connect if already connected
 25		 * 
 26		 * return true iff already connected
 27		 */
 28		public function connect(successFunc:Function, failureFunc:Function):Boolean {
 29			if (this.connecting) {
 30				throw new Error("Already attempting to connect");
 31			}
 32			if (this._nc != null) {
 33				if (this._nc.connected) {
 34					// already connected
 35					successFunc();
 36					return true;
 37				}
 38				else {
 39					// discard old connection anyway
 40					close();
 41				}
 42			}
 43			this._nc = new NetConnection();
 44			this._nc.client = this;
 45			this._nc.addEventListener(NetStatusEvent.NET_STATUS, function netStatusHandler(event:NetStatusEvent):void {
 46				connecting = false;
 47				switch (event.info.code) {
 48					case "NetConnection.Connect.Success":
 49						trace("Successfully connected: " + event.info.description);
 50						// write db config to the shared object
 51						PreferencesSharedObject.writeMediaServerURL(MediaConstants.MEDIA_SERVER_URL);
 52						successFunc();
 53						break;
 54					case "NetConnection.Connect.Failed":
 55						trace("Could not connect");
 56						close();
 57						failureFunc(event.info.code);
 58						break;
 59					default:
 60						trace(event.info.code + ": " + event.toString());
 61				}
 62			});
 63			this._nc.addEventListener(SecurityErrorEvent.SECURITY_ERROR, function securityErrorHandler(event:SecurityErrorEvent):void {
 64				connecting = false;
 65				trace("SecurityErrorEvent: " + event);
 66				close();
 67				failureFunc(event.text);
 68			});
 69			this.connecting = true;
 70			this._nc.connect(MediaConstants.MEDIA_SERVER_URL);
 71			return false;
 72		}
 73		
 74		public function isConnecting():Boolean {
 75			return this.connecting;
 76		}
 77		
 78		public function isConnected():Boolean {
 79			return this._nc != null && this._nc.connected && !isConnecting();
 80		}
 81		
 82		public function play(mediaURL:String, video:Video, statusFunc:Function, failureFunc:Function, start:int = -1, end:int = -1):void {
 83			if (!isConnected()) {
 84				throw new Error("Not connected to media server");
 85			}
 86			if (mediaStream != null) {
 87				if (mediaStream.isOpen() && mediaStream.mediaURL == mediaURL) {
 88					// great, we can reuse the existing mediaStream
 89					mediaStream.statusFunc = statusFunc;
 90					mediaStream.failureFunc = failureFunc;
 91				}
 92				else {
 93					mediaStream.close();
 94					mediaStream = null;
 95				}
 96			}
 97			if (mediaStream == null) {
 98				mediaStream = new MediaStream(mediaURL, this, statusFunc, failureFunc);
 99			}
100			mediaStream.play(video, start, end);
101		}
102			
103		public function close():void {
104			if (this.mediaStream != null) {
105				this.mediaStream.close();
106				this.mediaStream = null;
107			}
108			if (this._nc != null) {
109				trace("Closing connection");
110				this._nc.close();
111			}
112		}
113		
114		public function onBWDone():void{
115			trace("onBWDone");
116		}
117	}
118}