/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

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