|
1 IriSP.Widgets.PopcornPlayer = function(player, config) { |
|
2 IriSP.Widgets.Widget.call(this, player, config); |
|
3 }; |
|
4 |
|
5 IriSP.Widgets.PopcornPlayer.prototype = new IriSP.Widgets.Widget(); |
|
6 |
|
7 /* A Popcorn-based player for HTML5 Video, Youtube and Vimeo */ |
|
8 |
|
9 IriSP.Widgets.PopcornPlayer.prototype.defaults = { |
|
10 aspect_ratio: 14/9 |
|
11 } |
|
12 |
|
13 IriSP.Widgets.PopcornPlayer.prototype.draw = function() { |
|
14 |
|
15 |
|
16 if (typeof this.video === "undefined") { |
|
17 this.video = this.media.video; |
|
18 } |
|
19 |
|
20 if (this.url_transform) { |
|
21 this.video = this.url_transform(this.video); |
|
22 } |
|
23 |
|
24 if (!this.height) { |
|
25 this.height = Math.floor(this.width/this.aspect_ratio); |
|
26 this.$.css({ |
|
27 height: this.height |
|
28 }); |
|
29 } |
|
30 |
|
31 if (/^(https?:\/\/)?(www\.)?vimeo\.com/.test(this.video)) { |
|
32 |
|
33 /* VIMEO */ |
|
34 |
|
35 var _popcorn = Popcorn.vimeo(this.container, this.video); |
|
36 |
|
37 } else if (/^(https?:\/\/)?(www\.)?youtube\.com/.test(this.video)) { |
|
38 |
|
39 /* YOUTUBE */ |
|
40 |
|
41 var _urlparts = this.video.split(/[?&]/), |
|
42 _params = {}; |
|
43 for (var i = 1; i < _urlparts.length; i++) { |
|
44 var _ppart = _urlparts[i].split('='); |
|
45 _params[_ppart[0]] = decodeURIComponent(_ppart[1]); |
|
46 } |
|
47 _params.controls = 0; |
|
48 _params.modestbranding = 1; |
|
49 _url = _urlparts[0] + '?' + IriSP.jQuery.param(_params); |
|
50 |
|
51 var _popcorn = Popcorn.youtube(this.container, _url); |
|
52 |
|
53 } else { |
|
54 |
|
55 /* DEFAULT HTML5 */ |
|
56 |
|
57 var _tmpId = IriSP._.uniqueId("popcorn"), |
|
58 _videoEl = IriSP.jQuery('<video>'); |
|
59 _videoEl.attr({ |
|
60 id : _tmpId, |
|
61 width : this.width, |
|
62 height : this.height |
|
63 }); |
|
64 if(typeof this.video === "string"){ |
|
65 _videoEl.attr("src",this.video); |
|
66 } else { |
|
67 for (var i = 0; i < this.video.length; i++) { |
|
68 var _srcNode = IriSP.jQuery('<source>'); |
|
69 _srcNode.attr({ |
|
70 src: this.video[i].src, |
|
71 type: this.video[i].type |
|
72 }); |
|
73 _videoEl.append(_srcNode); |
|
74 } |
|
75 } |
|
76 this.$.html(_videoEl); |
|
77 var _popcorn = Popcorn("#" + _tmpId); |
|
78 } |
|
79 |
|
80 // Binding functions to Popcorn |
|
81 |
|
82 this.media.getCurrentTime = function() { |
|
83 return new IriSP.Model.Time(1000*_popcorn.currentTime()); |
|
84 } |
|
85 this.media.getVolume = function() { |
|
86 return _popcorn.volume(); |
|
87 } |
|
88 this.media.getPaused = function() { |
|
89 return _popcorn.media.paused; |
|
90 } |
|
91 this.media.getMuted = function() { |
|
92 return _popcorn.muted(); |
|
93 } |
|
94 this.media.setCurrentTime = function(_milliseconds) { |
|
95 return _popcorn.currentTime(_milliseconds / 1000); |
|
96 } |
|
97 this.media.setVolume = function(_vol) { |
|
98 return _popcorn.volume(_vol); |
|
99 } |
|
100 this.media.mute = function() { |
|
101 return _popcorn.muted(true); |
|
102 } |
|
103 this.media.unmute = function() { |
|
104 return _popcorn.muted(false); |
|
105 } |
|
106 this.media.play = function() { |
|
107 return _popcorn.play(); |
|
108 } |
|
109 this.media.pause = function() { |
|
110 return _popcorn.pause(); |
|
111 } |
|
112 |
|
113 // Binding Popcorn events to media |
|
114 |
|
115 var _media = this.media; |
|
116 _popcorn.on("timeupdate", function() { |
|
117 _media.trigger("timeupdate", _media.getCurrentTime()); |
|
118 }); |
|
119 |
|
120 function simpleEventBind(_eventname) { |
|
121 _popcorn.on(_eventname, function() { |
|
122 _media.trigger(_eventname); |
|
123 }); |
|
124 } |
|
125 |
|
126 simpleEventBind("play"); |
|
127 simpleEventBind("pause"); |
|
128 simpleEventBind("seeked"); |
|
129 simpleEventBind("loadedmetadata"); |
|
130 simpleEventBind("volumechange"); |
|
131 |
|
132 } |