Created
September 21, 2012 21:41
-
-
Save rbrancher/3764108 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var VimeoPlayer = function (player_id) { | |
this.player = document.getElementById(player_id); | |
this.player_ready = false; | |
this.play_when_ready = false; | |
this.play = function () { | |
if (this.player_ready !== true) { | |
this.play_when_ready = true; | |
} | |
this.command('play'); | |
} | |
this.command = function (action, value) { | |
var url = this.player.src, | |
data = {method: action} | |
if (value !== undefined) { | |
data.value = value; | |
} | |
return this.player.contentWindow.postMessage(JSON.stringify(data), url); | |
} | |
// Listen for messages from the player | |
if (window.addEventListener) { | |
window.addEventListener('message', onMessageReceived, false); | |
} else { | |
window.attachEvent('onmessage', onMessageReceived, false); | |
} | |
// Handle messages received from the player | |
function onMessageReceived(e) { | |
var data = JSON.parse(e.data); | |
switch (data.event) { | |
case 'ready': | |
if (typeof onReady === 'function') { onReady(); } | |
break; | |
case 'playProgress': | |
if (typeof onPlayProgress === 'function') { onPlayProgress(data.data); } | |
break; | |
case 'pause': | |
if (typeof onPause === 'function') { onPause(); } | |
break; | |
case 'finish': | |
if (typeof onFinish === 'function') { onFinish(); } | |
break; | |
} | |
} | |
// set event callbacks | |
var that = this; | |
var onReady = function () { | |
that.player_ready = true; | |
if (that.play_when_ready === true) { | |
that.command('play'); | |
} | |
} | |
var onFinish = function () { | |
that.command('unload'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment