/flowplayer/tags/flow_3_1_0/src/actionscript/org/flowplayer/view/Flowplayer.as

http://flowplayer-core.googlecode.com/ · ActionScript · 325 lines · 260 code · 33 blank · 32 comment · 43 complexity · 2d0b9024928689bf6e311329f71194ad MD5 · raw file

  1. /*
  2. * Copyright (c) 2008, 2009 Flowplayer Oy
  3. *
  4. * This file is part of Flowplayer.
  5. *
  6. * Flowplayer is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Flowplayer is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Flowplayer. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package org.flowplayer.view {
  20. import org.flowplayer.model.PlayerError;
  21. import org.flowplayer.controller.ResourceLoader;
  22. import org.flowplayer.config.Config;
  23. import org.flowplayer.config.ExternalInterfaceHelper;
  24. import org.flowplayer.controller.PlayListController;
  25. import org.flowplayer.flow_internal;
  26. import org.flowplayer.model.Callable;
  27. import org.flowplayer.model.Clip;
  28. import org.flowplayer.model.ClipEventType;
  29. import org.flowplayer.model.DisplayPluginModel;
  30. import org.flowplayer.model.DisplayProperties;
  31. import org.flowplayer.model.DisplayPropertiesImpl;
  32. import org.flowplayer.model.Loadable;
  33. import org.flowplayer.model.Playlist;
  34. import org.flowplayer.model.Plugin;
  35. import org.flowplayer.model.PluginEvent;
  36. import org.flowplayer.model.PluginEventType;
  37. import org.flowplayer.model.PluginModel;
  38. import org.flowplayer.model.Status;
  39. import org.flowplayer.util.NumberUtil;
  40. import org.flowplayer.util.ObjectConverter;
  41. import org.flowplayer.util.PropertyBinder;
  42. import org.flowplayer.view.FlowplayerBase;
  43. import org.flowplayer.view.Styleable;
  44. import flash.display.Stage;
  45. import flash.external.ExternalInterface;
  46. use namespace flow_internal;
  47. /**
  48. * @author api
  49. */
  50. public class Flowplayer extends FlowplayerBase {
  51. private var _canvas:StyleableSprite;
  52. public function Flowplayer(
  53. stage:Stage,
  54. pluginRegistry:PluginRegistry,
  55. panel:Panel,
  56. animationEngine:AnimationEngine,
  57. canvas:StyleableSprite,
  58. errorHandler:ErrorHandler,
  59. config:Config,
  60. playerSWFBaseURl:String) {
  61. super(stage, pluginRegistry, panel, animationEngine, errorHandler, config, playerSWFBaseURl);
  62. _canvas = canvas;
  63. }
  64. public function initExternalInterface():void {
  65. if (!ExternalInterface.available)
  66. log.info("ExternalInteface is not available in this runtime. JavaScript access will be disabled.");
  67. try {
  68. addCallback("getVersion", function():Array { return version; });
  69. addCallback("getPlaylist", function():Array { return convert(playlist.clips) as Array; });
  70. addCallback("getId", function():String { return id; });
  71. addCallback("play", genericPlay);
  72. addCallback("startBuffering", function():void { startBuffering(); });
  73. addCallback("stopBuffering", function():void { stopBuffering(); } );
  74. addCallback("isFullscreen", isFullscreen);
  75. addCallback("toggle", toggle);
  76. addCallback("getState", function():Number { return state.code; });
  77. addCallback("getStatus", function():Object { return convert(status); });
  78. addCallback("isPlaying", isPlaying);
  79. addCallback("isPaused", isPaused);
  80. var wrapper:WrapperForIE = new WrapperForIE(this);
  81. addCallback("stop", wrapper.fp_stop );
  82. addCallback("pause", wrapper.fp_pause );
  83. addCallback("resume", wrapper.fp_resume );
  84. addCallback("close", wrapper.fp_close );
  85. addCallback("getTime", function():Number { return status.time; });
  86. addCallback("mute", function():void { muted = true; });
  87. addCallback("unmute", function():void { muted = false; });
  88. addCallback("isMuted", function():Boolean { return muted; });
  89. addCallback("setVolume", function(value:Number):void { volume = value; });
  90. addCallback("getVolume", function():Number { return volume; });
  91. addCallback("seek", genericSeek);
  92. addCallback("getCurrentClip", function():Object {
  93. return new ObjectConverter(currentClip).convert(); });
  94. addCallback("getClip", function(index:Number):Object { return convert(playlist.getClip(index)); });
  95. addCallback("setPlaylist", function(clipObjects:Array):void { setPlaylist(_config.createClips(clipObjects)); });
  96. addCallback("addClip", function(clip:Object, index:int = -1):void { addClip(_config.createClip(clip), index); });
  97. addCallback("showError", showError);
  98. addCallback("loadPlugin", pluginLoad);
  99. addCallback("showPlugin", showPlugin);
  100. addCallback("hidePlugin", hidePlugin);
  101. addCallback("togglePlugin", togglePlugin);
  102. addCallback("animate", animate);
  103. addCallback("css", css);
  104. // return;
  105. addCallback("reset", reset);
  106. addCallback("fadeIn", fadeIn);
  107. addCallback("fadeOut", fadeOut);
  108. addCallback("fadeTo", fadeTo);
  109. addCallback("getPlugin", function(pluginName:String):Object {
  110. return new ObjectConverter(_pluginRegistry.getPlugin(pluginName)).convert();
  111. });
  112. addCallback("getRawPlugin", function(pluginName:String):Object {
  113. return _pluginRegistry.getPlugin(pluginName);
  114. });
  115. addCallback("invoke", invoke);
  116. addCallback("addCuepoints", addCuepoints);
  117. addCallback("updateClip", updateClip);
  118. addCallback("logging", logging);
  119. } catch (e:Error) {
  120. handleError(PlayerError.INIT_FAILED, "Unable to add callback to ExternalInterface");
  121. }
  122. }
  123. private function pluginLoad(name:String, url:String, properties:Object = null, callbackId:String = null):void {
  124. var loadable:Loadable = new Loadable(name, _config, url);
  125. if (properties) {
  126. new PropertyBinder(loadable, "config").copyProperties(properties);
  127. }
  128. loadPluginLoadable(loadable, callbackId != null ? createCallback(callbackId) : null);
  129. }
  130. private static function addCallback(methodName:String, func:Function):void {
  131. ExternalInterfaceHelper.addCallback("fp_" + methodName, func);
  132. }
  133. private function genericPlay(param:Object = null):void {
  134. if (param == null) {
  135. play();
  136. return;
  137. }
  138. if (param is Number) {
  139. _playListController.play(null, param as Number);
  140. return;
  141. }
  142. if (param is Array) {
  143. _playListController.playClips(_config.createClips(param as Array));
  144. return;
  145. }
  146. var clip:Clip = _config.createClip(param);
  147. if (! clip) {
  148. showError("cannot convert " + param + " to a clip");
  149. return;
  150. }
  151. play(clip);
  152. }
  153. private function genericSeek(target:Object):void {
  154. var percentage:Number = target is String ? NumberUtil.decodePercentage(target as String) : NaN;
  155. if (isNaN(percentage)) {
  156. seek(target is String ? parseInt(target as String) : target as Number);
  157. } else {
  158. seekRelative(percentage);
  159. }
  160. }
  161. private function css(pluginName:String, props:Object = null):Object {
  162. log.debug("css, plugin " + pluginName);
  163. if (pluginName == "canvas") {
  164. _canvas.css(props);
  165. return props;
  166. }
  167. return style(pluginName, props, false, 0);
  168. }
  169. private function convert(objToConvert:Object):Object {
  170. return new ObjectConverter(objToConvert).convert();
  171. }
  172. private function collectDisplayProps(props:Object, animatable:Boolean):Object {
  173. var result:Object = new Object();
  174. var coreDisplayProps:Array = [ "width", "height", "left", "top", "bottom", "right", "opacity" ];
  175. if (!animatable) {
  176. coreDisplayProps = coreDisplayProps.concat("display", "zIndex");
  177. }
  178. for (var propName:String in props) {
  179. if (coreDisplayProps.indexOf(propName) >= 0) {
  180. result[propName] = props[propName];
  181. // delete props[propName];
  182. }
  183. }
  184. return result;
  185. }
  186. private function animate(pluginName:String, props:Object, durationMillis:Number = 400, listenerId:String = null):Object {
  187. return style(pluginName, props, true, durationMillis, listenerId);
  188. }
  189. private function style(pluginName:String, props:Object, animate:Boolean, durationMillis:Number = 400, listenerId:String = null):Object {
  190. var plugin:Object = _pluginRegistry.getPlugin(pluginName);
  191. checkPlugin(plugin, pluginName, DisplayPluginModel);
  192. log.debug("going to animate plugin " + pluginName);
  193. var result:Object;
  194. if (props) {
  195. if (pluginName == 'play') {
  196. result = convert(_animationEngine.animateNonPanel(DisplayProperties(_pluginRegistry.getPlugin("screen")).getDisplayObject(), DisplayProperties(plugin).getDisplayObject(), collectDisplayProps(props, animate), durationMillis, createCallback(listenerId, plugin)));
  197. } else {
  198. result = convert(_animationEngine.animate(DisplayProperties(plugin).getDisplayObject(), collectDisplayProps(props, animate), durationMillis, createCallback(listenerId, plugin)));
  199. }
  200. } else {
  201. result = convert(plugin);
  202. }
  203. // check if plugin is Styleable and delegate to it
  204. if (plugin is DisplayProperties && DisplayProperties(plugin).getDisplayObject() is Styleable) {
  205. var newPluginProps:Object = Styleable(DisplayProperties(plugin).getDisplayObject())[animate ? "animate" : "css"](props);
  206. for (var prop:String in newPluginProps) {
  207. result[prop] = newPluginProps[prop];
  208. }
  209. }
  210. return result;
  211. }
  212. private function fadeOut(pluginName:String, durationMillis:Number = 400, listenerId:String = null):void {
  213. var props:DisplayProperties = prepareFade(pluginName, false);
  214. _animationEngine.fadeOut(props.getDisplayObject(), durationMillis, createCallback(listenerId, props));
  215. }
  216. private function fadeIn(pluginName:String, durationMillis:Number = 400, listenerId:String = null):void {
  217. var props:DisplayProperties = prepareFade(pluginName, true);
  218. if (pluginName == "play") {
  219. screen.showPlay();
  220. }
  221. _animationEngine.fadeIn(props.getDisplayObject(), durationMillis, createCallback(listenerId, props), pluginName != "play");
  222. }
  223. private function fadeTo(pluginName:String, alpha:Number, durationMillis:Number = 400, listenerId:String = null):void {
  224. var props:DisplayProperties = prepareFade(pluginName, true);
  225. if (pluginName == "play") {
  226. screen.showPlay();
  227. }
  228. _animationEngine.fadeTo(props.getDisplayObject(), alpha, durationMillis, createCallback(listenerId, props), pluginName != "play");
  229. }
  230. private function prepareFade(pluginName:String, show:Boolean):DisplayProperties {
  231. var plugin:Object = _pluginRegistry.getPlugin(pluginName);
  232. checkPlugin(plugin, pluginName, DisplayProperties);
  233. if (show) {
  234. var props:DisplayProperties = plugin as DisplayProperties;
  235. if (! props.getDisplayObject().parent || props.getDisplayObject().parent != _panel) {
  236. props.alpha = 0;
  237. }
  238. doShowPlugin(props.getDisplayObject(), props);
  239. }
  240. return plugin as DisplayProperties;
  241. }
  242. private function invoke(pluginName:String, methodName:String, args:Object = null):Object {
  243. var plugin:Callable = _pluginRegistry.getPlugin(pluginName) as Callable;
  244. checkPlugin(plugin, pluginName, Callable);
  245. try {
  246. log.debug("invoke()");
  247. if (plugin.getMethod(methodName).hasReturnValue) {
  248. log.debug("method has a return value");
  249. return plugin.invokeMethod(methodName, args is Array ? args as Array : [ args ]);
  250. } else {
  251. log.debug("method does not have a return value");
  252. plugin.invokeMethod(methodName, args is Array ? args as Array : [ args ]);
  253. }
  254. } catch (e:Error) {
  255. throw e;
  256. // handleError(PlayerError.PLUGIN_INVOKE_FAILED, "Error when invoking method '" + methodName + "', on plugin '" + pluginName + "'");
  257. }
  258. return "undefined";
  259. }
  260. private function addCuepoints(cuepoints:Array, clipIndex:int, callbackId:String):void {
  261. var clip:Clip = _playListController.playlist.getClip(clipIndex);
  262. var points:Array = _config.createCuepoints(cuepoints, callbackId, 1);
  263. if (! points || points.length == 0) {
  264. showError("unable to create cuepoints from " + cuepoints);
  265. }
  266. clip.addCuepoints(points);
  267. log.debug("clip has now cuepoints " + clip.cuepoints);
  268. }
  269. private function updateClip(clipObj:Object, clipIndex:int):void {
  270. var clip:Clip = _playListController.playlist.getClip(clipIndex);
  271. new PropertyBinder(clip, "customProperties").copyProperties(clipObj);
  272. clip.dispatch(ClipEventType.UPDATE);
  273. }
  274. //
  275. // private function createCallback(listenerId:String):Function {
  276. // if (! listenerId) return null;
  277. // return function():void {
  278. // new PluginEvent(PluginEventType.PLUGIN_EVENT, listenerId).fireExternal(_playerId);
  279. // };
  280. // }
  281. private function createCallback(listenerId:String, pluginArg:Object = null):Function {
  282. if (! listenerId) return null;
  283. return function(plugin:PluginModel = null):void {
  284. if (plugin || pluginArg is PluginModel) {
  285. PluginModel(pluginArg || plugin).dispatch(PluginEventType.PLUGIN_EVENT, listenerId);
  286. } else {
  287. new PluginEvent(PluginEventType.PLUGIN_EVENT, pluginArg is DisplayProperties ? DisplayProperties(pluginArg).name : pluginArg.toString(), listenerId).fireExternal(_playerId);
  288. }
  289. };
  290. }
  291. }
  292. }