Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 76ce381cad
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 516 lines (445 sloc) 15.543 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
package org.mindpirates.video.vimeo
{
import flash.display.DisplayObject;
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.external.ExternalInterface;
import flash.geom.Point;
import flash.net.URLRequest;
import flash.sensors.Accelerometer;
import flash.system.LoaderContext;
import flash.system.Security;
import flash.utils.Timer;
import flash.utils.describeType;

import org.mindpirates.video.VideoPlayerBase;
import org.mindpirates.video.events.VideoPlayerEvent;

/**
* <h3>VimeoPlayerBase</h3>
*
* <p>A wrapper class for Vimeo's video player (codenamed Moogaloop)
* that allows you to embed easily into any AS3 application.</p>
* <p>Edited to extend <code>org.mindpirates.video.VideoPlayer</code> and be extended by <code>org.mindpirates.video.vimeo.VimeoPlayer</code>
*
* <p>Example on how to use:
* <code><pre>var vimeo_player = new VimeoPlayer([YOUR_APPLICATIONS_CONSUMER_KEY], 2, 400, 300);
* vimeo_player.addEventListener(Event.COMPLETE, vimeoPlayerLoaded);
* addChild(vimeo_player);</pre></code>
* </p>
*
* <a href="http://vimeo.com/api/docs/moogaloop" target="_blank">http://vimeo.com/api/docs/moogaloop</a><br>
* <br>
* Register your application for access to the Moogaloop API at:<br>
* <br>
* <a href="http://vimeo.com/api/applications" target="_blank">http://vimeo.com/api/applications</a>
*
* @author (Edited by) Jovica Aleksic (jovi@mindpirates.org)
*/
public class VimeoPlayerBase extends VideoPlayerBase {

internal const MOOGALOOP_URL:String = "http://api.vimeo.com/moogaloop_api.swf";

// Assets
internal var container : Sprite = new Sprite(); // sprite that holds the player
internal var moogaloop : Object = false; // the player
internal var player_mask : Sprite = new Sprite(); // some sprites inside moogaloop go outside the bounds of the player. we use a mask to hide it

// Default variables
internal var player_width : int = 400;
internal var player_height : int = 300;
internal var api_version : int = 2;
internal var load_timer : Timer = new Timer(200);

// Events
// API v2
internal static const ERROR : String = 'error';
internal static const FINISH : String = 'finish';
internal static const LOAD_PROGRESS : String = 'loadProgress';
internal static const PAUSE : String = 'pause';
internal static const PLAY : String = 'play';
internal static const PLAY_PROGRESS : String = 'playProgress';
internal static const READY : String = 'ready';
internal static const SEEK : String = 'seek';

// API v1
internal static const ON_FINISH : String = 'onFinish';
internal static const ON_LOADING : String = 'onLoading';
internal static const ON_PAUSE : String = 'onPause';
internal static const ON_PLAY : String = 'onPlay';
internal static const ON_PROGRESS : String = 'onProgress';
internal static const ON_SEEK : String = 'onSeek';

// runtime variables
/**
* The ID of the current video.
*/
internal var _clip_id:int;

public function VimeoPlayerBase(oauth_key:String, clip_id:int, w:int, h:int, fp_version:String='10', api_version:int=2)
{
this.setDimensions(w, h);

Security.allowDomain('*');
Security.allowInsecureDomain('*');

var api_param : String = '&js_api=1';
this.api_version = api_version;
if (fp_version == '9')
{
this.api_version = 1;
}

api_param += '&api_version=' + this.api_version;

var url:String = MOOGALOOP_URL
+ "?oauth_key=" + oauth_key
+ "&clip_id=" + clip_id
+ "&width=" + w
+ "&height=" + h
+ "&fullscreen=1"
+ "&fp_version="+ fp_version
+ api_param
+ "&cache_buster=" + (new Date().time);

var request : URLRequest = new URLRequest(url);

var loaderContext : LoaderContext = new LoaderContext(true);

var loader : Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete, false, 0, true);
loader.load(request, loaderContext);
this.addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler, false, 0, true);

_clip_id = clip_id;

trace('VimeoPlayerBase', url);
}

override public function destroy() : void
{
trace(this, 'destroy()');
if (api_version == 2)
{
// API v2 Event Handlers
moogaloop.removeEventListener(ERROR, errorHandler);
moogaloop.removeEventListener(READY, readyHandler);
moogaloop.removeEventListener(PLAY, playHandler);
moogaloop.removeEventListener(PAUSE, pauseHandler);
moogaloop.removeEventListener(SEEK, seekHandler);
moogaloop.removeEventListener(LOAD_PROGRESS, loadProgressHandler);
moogaloop.removeEventListener(PLAY_PROGRESS, playProgressHandler);
moogaloop.removeEventListener(FINISH, finishHandler);
}
else
{
// API v1 Event Handlers
moogaloop.removeEventListener(ON_PLAY, onPlayHandler);
moogaloop.removeEventListener(ON_PAUSE, onPauseHandler);
moogaloop.removeEventListener(ON_SEEK, onSeekHandler);
moogaloop.removeEventListener(ON_LOADING, onLoadingHandler);
moogaloop.removeEventListener(ON_PROGRESS, onProgressHandler);
moogaloop.removeEventListener(ON_FINISH, onFinishHandler);
}

moogaloop.destroy();
if (container.contains(DisplayObject(moogaloop))) container.removeChild(DisplayObject(moogaloop));
if (this.contains(player_mask)) this.removeChild(player_mask);
if (this.contains(container)) this.removeChild(container);

stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
}

internal function setDimensions(w:int, h:int) : void
{
player_width = w;
player_height = h;
}

private function onComplete(e:Event) : void
{

trace(this , 'onComplete');


// Finished loading moogaloop
container.addChild(e.currentTarget.loader.content);
moogaloop = e.currentTarget.loader.content;
moogaloop.addEventListener(READY, readyHandler, false, 0, true);



if (api_version == 2)
{
// API v2 Event Handlers
moogaloop.addEventListener(PLAY, playHandler, false, 0, true);
moogaloop.addEventListener(PAUSE, pauseHandler, false, 0, true);
moogaloop.addEventListener(SEEK, seekHandler, false, 0, true);
moogaloop.addEventListener(LOAD_PROGRESS, loadProgressHandler, false, 0, true);
moogaloop.addEventListener(PLAY_PROGRESS, playProgressHandler, false, 0, true);
moogaloop.addEventListener(FINISH, finishHandler, false, 0, true);
trace('api v2 handlers attached')
}
else
{
// API v1 Event Handlers
moogaloop.addEventListener(ON_PLAY, onPlayHandler, false, 0, true);
moogaloop.addEventListener(ON_PAUSE, onPauseHandler, false, 0, true);
moogaloop.addEventListener(ON_SEEK, onSeekHandler, false, 0, true);
moogaloop.addEventListener(ON_LOADING, onLoadingHandler, false, 0, true);
moogaloop.addEventListener(ON_PROGRESS, onProgressHandler, false, 0, true);
moogaloop.addEventListener(ON_FINISH, onFinishHandler, false, 0, true);
trace('api v1 handlers attached')
}

// Create the mask for moogaloop
this.addChild(player_mask);
container.mask = player_mask;
this.addChild(container);

redrawMask();

load_timer.addEventListener(TimerEvent.TIMER, playerLoadedCheck);
load_timer.start();
}

/**
* Wait for Moogaloop to finish setting up
*/
private function playerLoadedCheck(e:TimerEvent) : void
{
if (moogaloop.player_loaded)
{
// Moogaloop is finished configuring
load_timer.stop();
load_timer.removeEventListener(TimerEvent.TIMER, playerLoadedCheck);


// remove moogaloop's mouse listeners listener
moogaloop.disableMouseMove();
if (stage)
{
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMove, false, 0, true);
}

//trace(describeType(moogaloop));
playerLoaded();
}
}

/**
* Invoked once the player has been loaded.
*/
internal function playerLoaded():void
{
//dispatchEvent(createVideoPlayerEvent(VideoPlayerEvent.PLAYER_READY));
dispatchEvent(new Event(Event.COMPLETE));
}

/**
* Fake the mouse move/out events for Moogaloop
*/
private function mouseMove(e:MouseEvent) : void
{
if (moogaloop && moogaloop.player_loaded)
{
var pos : Point = this.parent.localToGlobal(new Point(this.x, this.y));
if (e.stageX >= pos.x && e.stageX <= pos.x + this.player_width &&
e.stageY >= pos.y && e.stageY <= pos.y + this.player_height)
{
moogaloop.mouseMove(e);
}
else
{
moogaloop.mouseOut();
}
}
}

private function redrawMask() : void
{
with (player_mask.graphics)
{
beginFill(0x000000, 1);
drawRect(container.x, container.y, player_width, player_height);
endFill();
}
}

internal function doPlay():void
{
moogaloop.play();
}

internal function doPause():void
{
moogaloop.pause();
}

internal function doStop():void
{
seekTo(0);
doPause();
}

/**
* Returns the hilight color of the player.
*/
public function getColor():*
{
return uint(moogaloop.color);
}


/**
* returns duration of video in seconds
*/
internal function getDuration() : int
{
return moogaloop.duration;
}

/**
* Seek to specific loaded time in video (in seconds)
*/
internal function seekTo(time:int) : void
{
moogaloop.seek(time);
}

/**
* Change the primary color (i.e. 00ADEF)
*/
internal function changeColor(hex:String) : void
{
moogaloop.color = uint('0x' + hex);
}

/**
* Load in a different video
*/
internal function loadVideo(id:int) : void
{
_clip_id = id;
moogaloop.loadVideo(id);
doStop();
}

override public function setSize(w:int, h:int) : void
{
this.setDimensions(w, h);
moogaloop.setSize(w, h);
this.redrawMask();
}

/**
* ________________________________________________________________________________________________________
*
* Event Handlers
* ________________________________________________________________________________________________________
*
*/
internal function addedToStageHandler(event:Event) : void
{
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
this.addEventListener(Event.REMOVED_FROM_STAGE, removedFromStageHandler, false, 0, true);
}

internal function removedFromStageHandler(event:Event) : void
{
stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
this.removeEventListener(Event.REMOVED_FROM_STAGE, removedFromStageHandler);
}

/**
* ________________________________________________________________________________________________________
*
* API v2 Event Handlers
* ________________________________________________________________________________________________________
*
*/


/**
* Fired when an error occurs in the player.<br>
* Data Properties
* <ul>
* <li>title:String; Title of error message.</li>
* <li>message:String; Message associated with the error.</li>
* <li>link:String; (optional) Link to either the video or information about why the error occurred.
* <br>NOTE: This property is only included when a link is associated with the error, so make sure to check for its existence before referencing.</li>
* </ul>
*/
internal function errorHandler(event:Event) : void
{
trace('errorHandler');
}

/**
* Fired as soon as the player has finished requesting the clip info and is ready for playback, either on the initial load of the player or after a loadVideo request.
*/
internal function readyHandler(event:Event) : void
{
trace(this, 'readyHandler()');
dispatchEvent( createVideoPlayerEvent( VideoPlayerEvent.VIDEO_READY, event ) );
}

/**
* Fired when the video begins to play.
*/
internal function playHandler(event:Event) : void
{
trace('playHandler');
}

/**
* Fired when the video pauses.
*/
internal function pauseHandler(event:Event) : void
{
trace('pauseHandler');
}

/**
* Fired when the user seeks.<br>
* Data Properties
* <ul>
* <li>duration:Number; The duration of the video in seconds.</li>
* <li>percent:Number; The percent of the video that has been played.</li>
* <li>seconds:Number; The current time of playback for the video.</li>
* </ul>
*/
internal function seekHandler(event:Event) : void
{
trace('seekHandler');
}


/**
* Fired as the video is loading.<br>
* Data Properties
* <ul>
* <li>bytesLoaded:Number; Number of bytes loaded for the video.</li>
* <li>bytesTotal:Number; Total number of bytes for the video.</li>
* <li>duration:Number; The duration of the video in seconds.</li>
* <li>percent:Number; The percent loaded of the video file.</li>
*/
internal function loadProgressHandler(event:Event) : void
{
trace('loadProgressHandler');
}

/**
* Fired as the video is playing.<br>
* Data Properties:
* <ul>
* <li>duration:Number; The duration of the video in seconds.</li>
* <li>percent:Number; The percent of the video that has been played.</li>
* <li>seconds:Number; The current time of playback for the video.</li>
*/
internal function playProgressHandler(event:Event) : void
{
trace('playProgressHandler');
}

/**
* Fires when the video playback reaches the end.
*/
internal function finishHandler(event:Event) : void
{
trace('finishHandler');
}

/**
* ________________________________________________________________________________________________________
*
* API v1 Event Handlers
* ________________________________________________________________________________________________________
*
*/

/**
* @copy #playHandler()
*/
internal function onPlayHandler(event:Event) : void
{
trace('onPlayHandler');
}

/**
* @copy #pauseHandler()
*/
internal function onPauseHandler(event:Event) : void
{
trace('onPauseHandler');
}


/**
* @copy #seekHandler()
*/
internal function onSeekHandler(event:Event) : void
{
trace('onSeekHandler');
}

/**
* API v1 Load progress handler. Documentation may be imprecise.
* @see #loadProgressHandler()
*/
internal function onLoadingHandler(event:Event) : void
{
trace('onLoadingHandler');
}


/**
* API v1 play progress handler. Documentation may be imprecise.
* @see org.mindpirates.video.vimeo.VimeoPlayerBase#playProgressHandler() playProgressHandler()
*/
internal function onProgressHandler(event:Event) : void
{
trace('onProgressHandler');
}

/**
* @copy #finishHandler()
*/
internal function onFinishHandler(event:Event) : void
{
trace('onFinishHandler');
}
}
}
Something went wrong with that request. Please try again.