📄 launcher.as
字号:
/*
* Copyright (c) 2008, 2009 Flowplayer Oy
*
* This file is part of Flowplayer.
*
* Flowplayer is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Flowplayer is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Flowplayer. If not, see <http://www.gnu.org/licenses/>.
*/
package org.flowplayer.view {
import org.flowplayer.model.Canvas;
import org.flowplayer.model.Loadable;
import org.flowplayer.model.ProviderModel;
import org.flowplayer.config.Config;
import org.flowplayer.config.ConfigLoader;
import org.flowplayer.config.ExternalInterfaceHelper;
import org.flowplayer.config.VersionInfo;
import org.flowplayer.controller.NetStreamControllingStreamProvider;
import org.flowplayer.controller.PlayListController;
import org.flowplayer.controller.ResourceLoader;
import org.flowplayer.controller.ResourceLoaderImpl;
import org.flowplayer.flow_internal;
import org.flowplayer.model.Callable;
import org.flowplayer.model.Clip;
import org.flowplayer.model.ClipEvent;
import org.flowplayer.model.DisplayPluginModel;
import org.flowplayer.model.DisplayProperties;
import org.flowplayer.model.DisplayPropertiesImpl;
import org.flowplayer.model.EventDispatcher;
import org.flowplayer.model.Logo;
import org.flowplayer.model.PlayButtonOverlay;
import org.flowplayer.model.PlayerError;
import org.flowplayer.model.PlayerEvent;
import org.flowplayer.model.Playlist;
import org.flowplayer.model.Plugin;
import org.flowplayer.model.PluginError;
import org.flowplayer.model.PluginEvent;
import org.flowplayer.model.PluginModel;
import org.flowplayer.model.State;
import org.flowplayer.util.Arrange;
import org.flowplayer.util.Log;
import org.flowplayer.util.TextUtil;
import org.flowplayer.util.URLUtil;
import org.flowplayer.view.Panel;
import org.flowplayer.view.Screen;
import org.osflash.thunderbolt.Logger;
import flash.display.DisplayObject;
import flash.display.DisplayObjectContainer;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.net.URLRequest;
import flash.net.navigateToURL;
import flash.system.Capabilities;
import flash.system.Security;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.ui.Keyboard;
import flash.utils.Dictionary;
import flash.utils.Timer;
use namespace flow_internal;
public class Launcher extends StyleableSprite implements ErrorHandler {
private var _panel:Panel;
private var _screen:Screen;
private var _config:Config;
private var _flowplayer:Flowplayer;
private var _pluginRegistry:PluginRegistry;
private var _animationEngine:AnimationEngine;
private var _playButtonOverlay:PlayButtonOverlay;
private var _controlsModel:DisplayPluginModel;
private var _providers:Dictionary = new Dictionary();
private var _fullscreenManager:FullscreenManager;
private var _canvasLogo:Sprite;
private var _pluginLoader:PluginLoader;
private var _error:TextField;
private var _pluginsInitialized:Number = 0;
private var _numLoadablePlugins:int = -1;
private var _enteringFullscreen:Boolean;
private var _copyrightNotice:TextField;
[Frame(factoryClass="org.flowplayer.view.Preloader")]
public function Launcher() {
super("#canvas", this);
addEventListener(Event.ADDED_TO_STAGE, initPhase1);
}
private function initPhase1(event:Event):void {
createFlashVarsConfig();
Log.configure(_config.getLogConfiguration());
if (_config.playerId) {
Security.allowDomain(URLUtil.pageUrl);
}
_config.getPlaylist().onBeforeBegin(function(event:ClipEvent):void { hideErrorMessage(); });
loader = createNewLoader();
rootStyle = _config.canvas.style;
stage.addEventListener(Event.RESIZE, onStageResize);
setSize(stage.stageWidth, stage.stageHeight);
if (! VersionInfo.commercial) {
log.debug("Adding logo to canvas");
createLogoForCanvas();
}
log = new Log(this);
EventDispatcher.playerId = _config.playerId;
log.debug("security sandbox type: " + Security.sandboxType);
log.info(VersionInfo.versionInfo());
log.debug("creating Panel");
createPanel();
_pluginRegistry = new PluginRegistry(_panel);
log.debug("Creating animation engine");
createAnimationEngine(_pluginRegistry);
log.debug("creating play button overlay");
createPlayButtonOverlay();
log.debug("creating screen");
createScreen();
loadPluginsIfConfigured();
}
private function initPhase2(pluginsLoadedEvent:Event = null):void {
log.info("initPhase2, all plugins loaded");
_pluginLoader.removeEventListener(Event.COMPLETE, this.initPhase2);
log.debug("creating PlayListController");
_providers = _pluginLoader.providers;
var playListController:PlayListController = createPlayListController();
addPlayListListeners();
createFullscreenManager(playListController.playlist);
log.debug("creating Flowplayer API");
createFlowplayer(playListController);
addScreenToPanel();
if (!validateLicenseKey()) {
createLogoForCanvas();
resizeCanvasLogo();
}
log.debug("creating logo");
createLogo();
contextMenu = new ContextMenuBuilder(_config.playerId, _config.contextMenu).build();
log.debug("initializing ExternalInterface");
if (useExternalInterfade()) {
_flowplayer.initExternalInterface();
}
log.debug("calling onLoad to plugins");
_pluginRegistry.onLoad(_flowplayer);
}
private function initPhase3(event:Event = null):void {
log.info("initPhase3, all plugins initialized");
log.debug("Adding visible plugins to panel");
addPluginsToPanel(_pluginRegistry);
log.debug("arranging screen");
arrangeScreen();
log.debug("dispatching onLoad");
if (useExternalInterfade()) {
_flowplayer.dispatchEvent(PlayerEvent.load("player"));
}
log.debug("starting configured streams");
startStreams();
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
addListeners();
}
private function resizeCanvasLogo():void {
_canvasLogo.alpha = 1;
_canvasLogo.width = 150;
_canvasLogo.scaleY = _canvasLogo.scaleX;
arrangeCanvasLogo();
}
private function useExternalInterfade():Boolean {
log.debug("useExternalInteface: " + (_config.playerId != null));
return _config.playerId != null;
}
private function onStageResize(event:Event = null):void {
setSize(stage.stageWidth, stage.stageHeight);
arrangeCanvasLogo();
}
private function arrangeCanvasLogo():void {
if (!_canvasLogo) return;
_canvasLogo.x = 15;
_canvasLogo.y = stage.stageHeight - (_controlsModel ? _controlsModel.dimensions.height.toPx(stage.stageHeight) + 10 : 10) - _canvasLogo.height - _copyrightNotice.height;
_copyrightNotice.x = 12;
_copyrightNotice.y = _canvasLogo.y + _canvasLogo.height;
}
private function loadPluginsIfConfigured():void {
var plugins:Array = _config.getLoadables();
log.info("will load following plugins: ");
for (var i:Number = 0; i < plugins.length; i++) {
log.info("" + plugins[i]);
}
_pluginLoader = new PluginLoader(URLUtil.playerBaseUrl(loaderInfo), _pluginRegistry, this, useExternalInterfade(), onPluginLoad, onPluginLoadError);
_pluginLoader.addEventListener(Event.COMPLETE, initPhase2);
if (plugins.length == 0) {
log.debug("configuration has no plugins");
initPhase2();
} else {
log.debug("loading plugins and providers");
_pluginLoader.load(plugins);
}
}
private function onPluginLoad(event:PluginEvent):void {
var plugin:PluginModel = event.target as PluginModel;
log.info("plugin " + plugin + " initialized");
checkPluginsInitialized();
}
private function onPluginLoadError(event:PluginEvent):void {
if (! event.hasError(PluginError.INIT_FAILED)) return;
var plugin:PluginModel = event.target as PluginModel;
log.warn("load/init error on " + plugin);
_pluginRegistry.removePlugin(plugin);
checkPluginsInitialized();
}
private function checkPluginsInitialized():void {
var numPlugins:int = getLoadablePluginCount();
if (++_pluginsInitialized == numPlugins) {
log.info("all plugins initialized");
initPhase3();
}
log.info(_pluginsInitialized + " out of " + numPlugins + " plugins initialized (or have failed to load completely)");
}
private function getLoadablePluginCount():int {
return countLoadablePlugins();
// if (_numLoadablePlugins == -1) {
// _numLoadablePlugins = countLoadablePlugins();
// }
// return _numLoadablePlugins;
}
private function countLoadablePlugins():int {
var count:Number = 0;
var loadables:Array = _config.getLoadables();
for (var i:Number = 0; i < loadables.length; i++) {
var plugin:PluginModel = Loadable(loadables[i]).plugin;
var isNonAdHocPlugin:Boolean = (plugin is DisplayPluginModel && DisplayPluginModel(plugin).getDisplayObject() is Plugin) ||
plugin is ProviderModel && ProviderModel(plugin).getProviderObject() is Plugin;
if (Loadable(loadables[i]).loadFailed) {
log.debug("load failed for " + loadables[i]);
count++;
} else if (! plugin) {
log.debug("this plugin is not loaded yet");
count++;
} else if (isNonAdHocPlugin) {
log.debug("will wait for onLoad from plugin " + plugin);
count++;
} else {
log.debug("will NOT wait for onLoad from plugin " + Loadable(loadables[i]).plugin);
}
}
// +1 comes from the playbuttonoverlay
return count + (_playButtonOverlay ? 1 : 0);
}
private function playerSwfName():String {
var url:String = loaderInfo.url;
var lastSlash:Number = url.lastIndexOf("/");
return url.substring(lastSlash + 1, url.indexOf(".swf") + 4);
}
private function validateLicenseKey():Boolean {
try {
return LicenseKey.validate(root.loaderInfo.url, _flowplayer.version, _config.licenseKey, useExternalInterfade());
} catch (e:Error) {
log.warn("License key not accepted, will show flowplayer logo");
}
return false;
}
private function createFullscreenManager(playlist:Playlist):void {
_fullscreenManager = new FullscreenManager(stage, playlist, _panel, _pluginRegistry, _animationEngine);
}
public function showError(message:String):void {
if (! _panel) return;
if (! _config.showErrors) return;
if (_error && _error.parent == this) {
removeChild(_error);
}
_error = TextUtil.createTextField(false);
_error.background = true;
_error.backgroundColor = 0;
_error.textColor = 0xffffff;
_error.autoSize = TextFieldAutoSize.CENTER;
_error.multiline = true;
_error.wordWrap = true;
_error.text = message;
_error.selectable = true;
_error.width = stage.stageWidth - 40;
Arrange.center(_error, stage.stageWidth, stage.stageHeight);
addChild(_error);
createErrorMessageHideTimer();
}
private function createErrorMessageHideTimer():void {
var errorHideTimer:Timer = new Timer(4000, 1);
errorHideTimer.addEventListener(TimerEvent.TIMER_COMPLETE, hideErrorMessage);
errorHideTimer.start();
}
private function hideErrorMessage(event:TimerEvent = null):void {
if (_error && _error.parent == this) {
if (_animationEngine) {
_animationEngine.fadeOut(_error, 1000, function():void { removeChild(_error); });
} else {
removeChild(_error);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -