PluginsLoader

6 views
Skip to first unread message

Mario Vieira

unread,
Nov 23, 2010, 6:24:30 PM11/23/10
to reops
Hi guys,

I looked at the Reops player for the plugin loading, but I ended up
writing a class for it, which is the following.

Plugins loading is an asynchronous call, so this class loads one by
one, and has a timer for the case of a plugin taking longer than it
wished to load. It also keeps the reference of the loaded plugin(s)
for a check upon factoring the media element (so it can added any
necessary metadata - for the GTrack page name only for now).

Since you guys put a lot of work of there, and I totally support the
open source I hope this untested class helps (seen some to do comments
on it :) !

package net.mariovieira.flashplayer.osmf.helpers
{
import com.realeyes.osmfplayer.model.Plugin;

import flash.events.EventDispatcher;
import flash.events.IEventDispatcher;
import flash.events.TimerEvent;
import flash.utils.Timer;

import net.mariovieira.flashplayer.osmf.events.PluginsLoaderEvent;
import
net.mariovieira.flashplayer.utils.formatter.events.EventsEnums;

import org.osmf.events.MediaFactoryEvent;
import org.osmf.media.DefaultMediaFactory;
import org.osmf.media.MediaFactory;
import org.osmf.media.MediaResourceBase;


[Event("net.mariovieira.flashplayer.osmf.events.PluginsLoaderEvent",
name="onPlugins" )]

/**
*
* @private
*
*/
public class PluginLoader extends EventDispatcher
{
private const LOADER_TIME_OUT:Number = 2000; //per plugin

private var _mediaFactory:DefaultMediaFactory;
private var _plugins:Vector.<Plugin>;
private var _pluginsLoaded:Vector.<MediaResourceBase>;

private var _totalIndex:int;
private var _currentIndex:int;
private var _timeoutLoader:Timer;

/**
* Modification:
*
* This was changed to create a MediaFactory instance, and
return it
* (first thing happens is loading the plugins in my player)
*
*/
public function PluginLoader()
{
_plugins = new Vector.<Plugin>();
_timeoutLoader = new Timer(LOADER_TIME_OUT, 1);
}

public function get mediaFactory():DefaultMediaFactory
{
return _mediaFactory;
}

public function get pluginLoaded():Vector.<MediaResourceBase>
{
return _pluginsLoaded;
}

public function addPlugin( plugin:Plugin ):void
{
trace('ADD PLUGIN - '+plugin.resource);
_plugins.push( plugin );
}

public function loadPlugins():void
{
trace('LOAD PLUGIN ( S )');
reset();
_totalIndex = _plugins.length;
_mediaFactory = new DefaultMediaFactory();

loadNextPlugin();
}

protected function loadNextPlugin():void
{
if(_currentIndex < _plugins.length)
{
addNotRemoveEvents(true);
_currentIndex ++;

_mediaFactory.loadPlugin( _plugins[0].resource );
runTimer();
}
else
{
addNotRemoveEvents(false);
dispatchEvent(new PluginsLoaderEvent());
}
}

private function runTimer():void
{
_timeoutLoader.reset();
_timeoutLoader.start();
}

private function onTimed(e:TimerEvent):void
{
addNotRemoveEvents(false);
loadNextPlugin();
}

protected function
onPluginLoaded( event:MediaFactoryEvent ):void
{
//needs to remove the event before it adds it again
if(event.type == MediaFactoryEvent.PLUGIN_LOAD_ERROR)
addNotRemoveEvents(false);
_pluginsLoaded.push(event.resource);

loadNextPlugin();
}

protected function reset():void
{
_pluginsLoaded = new Vector.<MediaResourceBase>();
_totalIndex = 0;
_currentIndex = 0;
}

public function hasPlugins():Boolean
{
return (_plugins.length > 0);
}

protected function
addNotRemoveEvents(addNotRemove:Boolean):void
{
var functionName:String =
EventsEnums.getAddOrRemoveEvent(addNotRemove);

_mediaFactory[functionName]
( MediaFactoryEvent.PLUGIN_LOAD , onPluginLoaded );
//on fails skips and loads the next one
_mediaFactory[functionName]
( MediaFactoryEvent.PLUGIN_LOAD_ERROR, onPluginLoaded );
_timeoutLoader[functionName]( TimerEvent.TIMER_COMPLETE,
onTimed );
}
}
}

turbidity

unread,
Nov 24, 2010, 3:32:44 PM11/24/10
to reops
Thanks, man! That's a great idea. That's definitely an issue we've
seen with plug-ins hosted by third parties.
Reply all
Reply to author
Forward
0 new messages