Menu

[r302]: / branches / hammer / player / StrobeMediaPlayback / html-template / lib / jquery.strobemediaplaybackhtml5.js  Maximize  Restore  History

Download this file

311 lines (265 with data), 11.8 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
/*****************************************************
*
* Copyright 2010 Adobe Systems Incorporated. All Rights Reserved.
*
*****************************************************
* The contents of this file are subject to the Berkeley Software Distribution (BSD) Licence
* (the "License"); you may not use this file except in
* compliance with the License.
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
*
* The Initial Developer of the Original Code is Adobe Systems Incorporated.
* Portions created by Adobe Systems Incorporated are Copyright (C) 2010 Adobe Systems
* Incorporated. All Rights Reserved.
*
*****************************************************/
/**
*
*/
(function($, undefined){
/**
*
*/
var StrobeMediaPlaybackHtml5 = function(element, options){
this.$window = $(window);
this.$document = $(document);
this.element = element;
this.$element = $(element);
this.options = $.extend({}, $.fn.strobemediaplaybackhtml5.defaults, options);
};
var strobeMediaPlaybackHtml5Methods = {
initialize: function(){
$(document).bind('webkitfullscreenchange', this, this.onFullscreenChange);
this.$element.bind("mousemove", this, this.onMouseMove);
this.$player = this.$element.find("video"); //$("#" + this.options.id)
this.player = this.$player.get(0);
this.$player.bind("timeupdate", this, this.onTimeUpdate);
this.$player.bind('play', this, this.onPlay);
this.$player.bind("playing", this, this.onPlaying);
this.$player.bind('pause', this, this.onPause);
this.$player.bind('ended', this, this.onEnded);
this.$player.bind("loadeddata", this, this.onLoadedData);
this.$player.bind("loadedmetadata", this, this.onMetaDataLoaded);
this.$player.bind("progress", this, this.onProgress);
this.$player.bind("click", this, this.onClick);
this.$player.bind("dblclick", this, this.onDoubleClick);
this.$player.bind("contextmenu", this, function(event){ event.preventDefault(); }); //disable context menu
this.hidedelaytimeout, this.isdragging, this.trackswidth;
this.controlbar = this.$element.find(this.options.controlbarselector);
this.progressbar = this.controlbar.find(this.options.progressbarselector);
this.progressbar.bind('click', this, this.onSeekClick);
this.tracks = this.progressbar.find(this.options.tracksselector);
this.seekbar = this.progressbar.find(this.options.seekbarselector);
this.playedbar = this.progressbar.find(this.options.playedbarselector);
this.bufferbar = this.progressbar.find(this.options.bufferedbarselector);
this.playtoggle = this.controlbar.find(this.options.playtoggleselector);
this.playtoggle.bind('click', this, this.onPlayToggleClick);
this.playtoggle.bind("mousedown mouseup touchstart touchend", this, this.onButtonHover);
this.slider = this.controlbar.find(this.options.sliderselector);
this.slider.draggable({disabled: false, containment: "parent", axis: "x"});
this.slider.bind("dragstart", this, this.onSliderDragStart);
this.slider.bind("drag", this, this.onSliderDragging);
this.slider.bind("dragstop", this, this.onSliderDragStop);
this.slider.bind("mousedown mouseup touchstart touchend", this, this.onButtonHover);
this.fullview = this.controlbar.find(this.options.fullviewselector);
this.fullview.bind("click", this, this.onFullViewClick);
this.fullview.bind("mousedown mouseup touchstart touchend", this, this.onButtonHover);
//disable the fullview button until metadata is loaded; necessary for iPad native fullscreen to work
this.fullview.addClass("disabled");
this.$player.attr("preload", "true"); //start loading the video, if not already started
this.currenttime = this.controlbar.find(this.options.currenttimeselector);
this.duration = this.controlbar.find(this.options.durationtimeselector);
this.errorwindow = this.$element.find(this.options.errorwindowselector);
this.options.originalWidth = this.player.clientWidth;
if(this.options.autoplay) this.player.play();
},
onSliderDragStart: function(event){
event.data.isdragging = true;
event.data.seekbar.css("width", event.data.playedbar.width()+"px").show();
},
onSliderDragging: function(event, ui){
event.data.seekbar.css("width", ui.position.left+"px");
},
onSliderDragStop: function(event, ui){
event.data.isdragging = false;
event.data.seekbar.hide();
event.data.player.currentTime = event.data.player.duration*((ui.position.left+event.data.slider.width()/2)/$(this).parent().width());
event.data.onTimeUpdate(event);
},
onProgress: function(event){
try{
var start = event.target.buffered.start();
var end = event.target.buffered.end()
var duration = event.target.duration;
event.data.bufferbar.css("width", ((end/duration)*100)+"%");
}catch(exception){}
},
onPlay: function(event){
event.data.playtoggle.addClass('paused');
},
onPlaying: function(event){
event.data.slider.draggable("option", "disabled", false);
},
onPause: function(event){
event.data.playtoggle.removeClass('paused');
},
onEnded: function(event){
event.data.onPause(event);
event.data.showControls();
},
onLoadedData: function(event){
event.data.bufferbar.css("width", "100%");
event.data.showControls();
event.data.duration.html(formatTimeStatus(0, event.target.duration)[1]);
event.data.trackswidth = event.data.tracks.width();
},
onMetaDataLoaded: function(event){
event.data.fullview.removeClass("disabled");
},
onMouseMove: function(event){
event.data.showControls();
if(!event.data.player.paused && !event.data.player.ended){
clearTimeout(event.data.hidedelaytimeout);
event.data.hidedelaytimeout = setTimeout(function(){event.data.hideControls(event);}, event.data.options.hidedelay);
}
},
onButtonHover: function(event){
$(this).toggleClass("hover");
},
onPlayToggleClick: function(event){
if(event.data.player.paused || event.data.player.ended) event.data.player.play();
else event.data.player.pause();
},
onSeekClick: function(event){
var time = event.data.player.duration * ((event.clientX - event.data.progressbar.offset().left) / event.data.progressbar.outerWidth());
if(time > 0) event.data.player.currentTime = time;
if(event.data.player.paused) event.data.onPause(event);
else event.data.onPlay(event);
},
onClick: function(event){
if(event.data.player.ended) event.data.hideControls(event);
else event.data.onMouseMove(event);
},
onDoubleClick: function(event){
event.data.onFullViewClick(event);
},
onFullViewClick: function(event){
try{
if(event.data.fullview.hasClass("disabled")) return; //do not do anything if the button is disabled
if(navigator.userAgent.match(/(Macintosh|Windows|Safari|Version\/5\.[1-9])/gi).length >= 3){ //at a minimum match one of the OSes, Safari, and the version
if($(document).context.webkitIsFullScreen) $(document).context.webkitCancelFullScreen();
else event.data.$element.context.webkitRequestFullScreen();
}else event.data.player.webkitEnterFullScreen();
}catch(error){
event.data.onFullscreenChange(event);
}
},
onFullscreenChange: function(event){
event.data.$element.toggleClass(event.data.options.fullscreenclass);
event.data.fullview.toggleClass(event.data.options.fullviewactiveclass);
event.data.trackswidth = event.data.tracks.width();
if(event.data.player.ended) event.data.onTimeUpdate(event);
},
onTimeUpdate: function(event){
var percent = event.data.player.currentTime/event.data.player.duration;
var px = percent*event.data.trackswidth;
event.data.playedbar.css("width", px+"px");
if(!event.data.isdragging) event.data.slider.css("left", px+"px");
var times = formatTimeStatus(event.data.player.currentTime, event.data.player.duration);
event.data.currenttime.html(times[0]);
event.data.duration.html(times[1]);
},
onError: function(event){
var message;
switch (event.target.error.code) {
case event.target.error.MEDIA_ERR_ABORTED:
message = 'You aborted the video playback.';
break;
case event.target.error.MEDIA_ERR_NETWORK:
message = 'A network error caused the video download to fail part-way.';
break;
case event.target.error.MEDIA_ERR_DECODE:
message = 'The video playback was aborted due to a corruption problem or because the video used features your browser did not support.';
break;
case event.target.error.MEDIA_ERR_SRC_NOT_SUPPORTED:
message = 'The video could not be loaded, either because the server or network failed or because the format is not supported.';
break;
default:
message = 'An unknown error occurred.';
break;
}
event.data.errorwindow.html(message);
event.data.errorwindow.show();
},
showControls: function(){
if(this.controlbar.is(":visible")) return;
this.controlbar.fadeIn(this.options.fadeinspeed);
},
hideControls: function(event){
if(event.data.player.paused && !event.data.player.ended) return;
event.data.controlbar.fadeOut(event.data.options.fadeoutspeed);
}
};
StrobeMediaPlaybackHtml5.prototype = strobeMediaPlaybackHtml5Methods;
/**
* jQuery plugin hook
*/
$.fn.strobemediaplaybackhtml5 = function(options){
var instances = [], i;
var result = this.each(function(){
instances.push(new StrobeMediaPlaybackHtml5(this, options));
});
for (i = 0; i < instances.length; i++) {
instances[i].initialize();
}
return result;
};
/**
* jQuery plugin defaults
*/
$.fn.strobemediaplaybackhtml5.defaults = {
autoplay: false,
hidedelay: 6000,
fadeinspeed: "fast",
fadeoutspeed: 500,
controlbarselector: ".controls",
progressbarselector: ".progress",
tracksselector: ".tracks",
sliderselector: ".slider",
seekbarselector: ".seeking",
playedbarselector: ".played",
bufferedbarselector: ".buffered",
playtoggleselector: ".icon.playtoggle",
currenttimeselector: ".timestamp.current",
durationtimeselector: ".timestamp.duration",
errorwindowselector: ".errorwindow",
fullviewselector: ".icon.fullview",
fullscreenclass: "fullscreen",
fullviewactiveclass: "fullscreen"
};
function formatTimeStatus(currentPosition, totalDuration){
var h;
var m;
var s;
function prettyPrintSeconds(seconds, leadingMinutes, leadingHours){
seconds = Math.floor(isNaN(seconds) ? 0 : Math.max(0, seconds));
h = Math.floor(seconds / 3600);
m = Math.floor(seconds % 3600 / 60);
s = seconds % 60;
return ((h > 0 || leadingHours) ? (h + ":") : "") +
(((h > 0 || leadingMinutes) && m < 10) ? "0" : "") +
m +
":" +
(s < 10 ? "0" : "") +
s;
}
var totalDurationString = prettyPrintSeconds(totalDuration);
var currentPositionString = prettyPrintSeconds(currentPosition, h > 0 || m > 9, h > 0);
return [currentPositionString, totalDurationString];
}
})(jQuery);
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.