-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathshelby-player.js
More file actions
85 lines (73 loc) · 2.48 KB
/
Copy pathshelby-player.js
File metadata and controls
85 lines (73 loc) · 2.48 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
/*
* Shelby Player API
* authors : ['@mkrecny', '@henrysztul']
*/
/*
* ShelbyPlayer
* @param containerDiv : string : the jQuery selector string of the player's containing div
* @param onStateChange : function : (optional) the function to execute when any player state changes
* @param renderOptions : object : (optional) a map of rendering options for the player
*/
var ShelbyPlayer = function(options, onStateChange){
var self = this;
this.options = options;
this.validOptions = ['container', 'sidebar', 'shade'];
this.rootUri = 'http://alpha.shelby.tv/';
this.iframe = $('<iframe id="'+options.container+'-iframe" style="width:100%;height:100%"></iframe>')[0];
jQuery('#'+options.container).append(this.iframe);
// this.iframe message handler
window.addEventListener("message", function(event){
data = JSON.parse(event.data);
if (data.id === options.container){
if(data.playerLoaded){
this.playerLoaded = data.msg.playerLoaded;
}
onStateChange(data, self);
}
}, false);
};
/*
* ShelbyPlayer.playBroadcast : start playing a broadcast
* @param channelId : string : the channel id of the broadcast to play
* @param broadcastId : string : the broadcast id of the broadcast to play
* returns : string : the new url of the iframes src
*/
ShelbyPlayer.prototype.playBroadcast = function(channelId, broadcastId){
var self = this;
var newSrc = this.rootUri+'#!/channels/'+channelId+'/broadcasts/'+broadcastId+'/iframe/';
if (this.options){
Object.keys(this.options).forEach(function(k){
if (self.validOptions.indexOf(k)===-1){
delete this.options[k];
}
});
newSrc+=escape(JSON.stringify(this.options));
}
return this.iframe.src = newSrc;
};
/*
* ShelbyPlayer.isPlayerLoaded : determine if (a) player is currently loaded
* returns : boolean
*/
ShelbyPlayer.prototype.isPlayerLoaded = function(){
return this.playerLoaded;
};
/*
* ShelbyPlayer.togglePlay : if playing, pause, if paused, play
*/
ShelbyPlayer.prototype.togglePlay = function(){
this._postMessage('iframe:toggleplay');
};
/*
* ShelbyPlayer.toggleMute : if unmuted, mute, if muted, unmute
*/
ShelbyPlayer.prototype.toggleMute = function(){
this._postMessage('iframe:togglemute');
};
/*
* ShelbyPlayer._postMessage : post a message to this.iframe
* @param message : string : the message to post this.iframe
*/
ShelbyPlayer.prototype._postMessage = function(message){
return this.iframe.contentWindow.postMessage(message, this.rootUri);
};