comparison johndyer-mediaelement-13fa20a/src/js/mep-feature-progress.js @ 0:032bc65ebafc

added core components
author George Fazekas <gyorgy.fazekas@eecs.qmul.ac.uk>
date Wed, 06 Mar 2013 15:45:48 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:032bc65ebafc
1 (function($) {
2 // progress/loaded bar
3 MediaElementPlayer.prototype.buildprogress = function(player, controls, layers, media) {
4
5 $('<div class="mejs-time-rail">'+
6 '<span class="mejs-time-total">'+
7 '<span class="mejs-time-loaded"></span>'+
8 '<span class="mejs-time-current"></span>'+
9 '<span class="mejs-time-handle"></span>'+
10 '<span class="mejs-time-float">' +
11 '<span class="mejs-time-float-current">00:00</span>' +
12 '<span class="mejs-time-float-corner"></span>' +
13 '</span>'+
14 '</span>'+
15 '</div>')
16 .appendTo(controls);
17
18 var
19 t = this,
20 total = controls.find('.mejs-time-total'),
21 loaded = controls.find('.mejs-time-loaded'),
22 current = controls.find('.mejs-time-current'),
23 handle = controls.find('.mejs-time-handle'),
24 timefloat = controls.find('.mejs-time-float'),
25 timefloatcurrent = controls.find('.mejs-time-float-current'),
26 handleMouseMove = function (e) {
27 // mouse position relative to the object
28 var x = e.pageX,
29 offset = total.offset(),
30 width = total.outerWidth(),
31 percentage = 0,
32 newTime = 0;
33
34
35 if (x > offset.left && x <= width + offset.left && media.duration) {
36 percentage = ((x - offset.left) / width);
37 newTime = (percentage <= 0.02) ? 0 : percentage * media.duration;
38
39 // seek to where the mouse is
40 if (mouseIsDown) {
41 media.setCurrentTime(newTime);
42 }
43
44 // position floating time box
45 var pos = x - offset.left;
46 timefloat.css('left', pos);
47 timefloatcurrent.html( mejs.Utility.secondsToTimeCode(newTime) );
48 }
49 },
50 mouseIsDown = false,
51 mouseIsOver = false;
52
53 // handle clicks
54 //controls.find('.mejs-time-rail').delegate('span', 'click', handleMouseMove);
55 total
56 .bind('mousedown', function (e) {
57 mouseIsDown = true;
58 handleMouseMove(e);
59 return false;
60 });
61
62 controls.find('.mejs-time-rail')
63 .bind('mouseenter', function(e) {
64 mouseIsOver = true;
65 })
66 .bind('mouseleave',function(e) {
67 mouseIsOver = false;
68 });
69
70 $(document)
71 .bind('mouseup', function (e) {
72 mouseIsDown = false;
73 //handleMouseMove(e);
74 })
75 .bind('mousemove', function (e) {
76 if (mouseIsDown || mouseIsOver) {
77 handleMouseMove(e);
78 }
79 });
80
81 // loading
82 media.addEventListener('progress', function (e) {
83 player.setProgressRail(e);
84 player.setCurrentRail(e);
85 }, false);
86
87 // current time
88 media.addEventListener('timeupdate', function(e) {
89 player.setProgressRail(e);
90 player.setCurrentRail(e);
91 }, false);
92
93
94 // store for later use
95 t.loaded = loaded;
96 t.total = total;
97 t.current = current;
98 t.handle = handle;
99 }
100 MediaElementPlayer.prototype.setProgressRail = function(e) {
101
102 var
103 t = this,
104 target = (e != undefined) ? e.target : t.media,
105 percent = null;
106
107 // newest HTML5 spec has buffered array (FF4, Webkit)
108 if (target && target.buffered && target.buffered.length > 0 && target.buffered.end && target.duration) {
109 // TODO: account for a real array with multiple values (only Firefox 4 has this so far)
110 percent = target.buffered.end(0) / target.duration;
111 }
112 // Some browsers (e.g., FF3.6 and Safari 5) cannot calculate target.bufferered.end()
113 // to be anything other than 0. If the byte count is available we use this instead.
114 // Browsers that support the else if do not seem to have the bufferedBytes value and
115 // should skip to there. Tested in Safari 5, Webkit head, FF3.6, Chrome 6, IE 7/8.
116 else if (target && target.bytesTotal != undefined && target.bytesTotal > 0 && target.bufferedBytes != undefined) {
117 percent = target.bufferedBytes / target.bytesTotal;
118 }
119 // Firefox 3 with an Ogg file seems to go this way
120 else if (e && e.lengthComputable && e.total != 0) {
121 percent = e.loaded/e.total;
122 }
123
124 // finally update the progress bar
125 if (percent !== null) {
126 percent = Math.min(1, Math.max(0, percent));
127 // update loaded bar
128 if (t.loaded && t.total) {
129 t.loaded.width(t.total.width() * percent);
130 }
131 }
132 }
133 MediaElementPlayer.prototype.setCurrentRail = function() {
134
135 var t = this;
136
137 if (t.media.currentTime != undefined && t.media.duration) {
138
139 // update bar and handle
140 if (t.total && t.handle) {
141 var
142 newWidth = t.total.width() * t.media.currentTime / t.media.duration,
143 handlePos = newWidth - (t.handle.outerWidth(true) / 2);
144
145 t.current.width(newWidth);
146 t.handle.css('left', handlePos);
147 }
148 }
149
150 }
151
152 })(mejs.$);