OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * @fileoverview MediaControls class implements media playback controls | 6 * @fileoverview MediaControls class implements media playback controls |
7 * that exist outside of the audio/video HTML element. | 7 * that exist outside of the audio/video HTML element. |
8 */ | 8 */ |
9 | 9 |
10 /** | 10 /** |
11 * @param {HTMLMediaElement} mediaElement The media element to control. | 11 * @param {HTMLMediaElement} mediaElement The media element to control. |
12 * @param {HTMLElement} containerElement The container for the controls. | 12 * @param {HTMLElement} containerElement The container for the controls. |
| 13 * @param {function} fullScreenToggle Function to toggle the fullscreen mode. |
13 * @constructor | 14 * @constructor |
14 */ | 15 */ |
15 function MediaControls(mediaElement, containerElement) { | 16 function MediaControls(mediaElement, containerElement, fullScreenToggle) { |
16 this.media_ = mediaElement; | 17 this.media_ = mediaElement; |
17 this.container_ = containerElement; | 18 this.container_ = containerElement; |
18 this.document_ = this.container_.ownerDocument; | 19 this.document_ = this.container_.ownerDocument; |
19 | 20 |
20 this.setupPlaybackControls_(); | 21 this.setupPlaybackControls_(fullScreenToggle); |
21 | 22 |
22 this.setupMediaEvents_(); | 23 this.setupMediaEvents_(); |
23 } | 24 } |
24 | 25 |
25 /** | 26 /** |
26 * Load the given url into the media element. | 27 * Load the given url into the media element. |
27 * | 28 * |
28 * @param {string} url | 29 * @param {string} url |
29 */ | 30 */ |
30 MediaControls.prototype.load = function(url) { | 31 MediaControls.prototype.load = function(url) { |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 return button; | 91 return button; |
91 }; | 92 }; |
92 | 93 |
93 // The default range of 100 is too coarse for the media progress slider. | 94 // The default range of 100 is too coarse for the media progress slider. |
94 // 1000 should be enough as the entire media controls area is never longer | 95 // 1000 should be enough as the entire media controls area is never longer |
95 // than 800px. | 96 // than 800px. |
96 MediaControls.PROGRESS_RANGE = 1000; | 97 MediaControls.PROGRESS_RANGE = 1000; |
97 | 98 |
98 /** | 99 /** |
99 * Create playback controls in DOM. | 100 * Create playback controls in DOM. |
| 101 * |
| 102 * @param {function} fullScreenToggle Function to toggle the fullscreen mode. |
100 */ | 103 */ |
101 MediaControls.prototype.setupPlaybackControls_ = function() { | 104 MediaControls.prototype.setupPlaybackControls_ = function(fullScreenToggle) { |
102 this.playButton_ = this.createButton_( | 105 this.playButton_ = this.createButton_( |
103 'play', this.onPlayButtonClick_.bind(this)); | 106 'play', this.onPlayButtonClick_.bind(this)); |
104 | 107 |
105 this.progress_ = new MediaControls.Slider( | 108 this.progress_ = new MediaControls.Slider( |
106 this.createControl_('progress'), | 109 this.createControl_('progress'), |
107 0, /* value */ | 110 0, /* value */ |
108 MediaControls.PROGRESS_RANGE); | 111 MediaControls.PROGRESS_RANGE); |
109 | 112 |
110 this.progress_.getContainer().addEventListener( | 113 this.progress_.getContainer().addEventListener( |
111 'mousemove', this.onProgressMouseMove_.bind(this)); | 114 'mousemove', this.onProgressMouseMove_.bind(this)); |
(...skipping 27 matching lines...) Expand all Loading... |
139 100 /* range */); | 142 100 /* range */); |
140 this.onVolumeChange_(); | 143 this.onVolumeChange_(); |
141 | 144 |
142 this.volume_.getInput_().addEventListener( | 145 this.volume_.getInput_().addEventListener( |
143 'change', this.onVolumeChange_.bind(this)); | 146 'change', this.onVolumeChange_.bind(this)); |
144 this.volume_.getInput_().addEventListener( | 147 this.volume_.getInput_().addEventListener( |
145 'mousedown', this.onVolumeMouseDown_.bind(this)); | 148 'mousedown', this.onVolumeMouseDown_.bind(this)); |
146 | 149 |
147 this.volume_.setValue(this.media_.volume); | 150 this.volume_.setValue(this.media_.volume); |
148 | 151 |
149 this.fullscreenButton_ = this.createButton_( | 152 if (fullScreenToggle) { |
150 'fullscreen', this.onFullscreenButtonClick_.bind(this)); | 153 this.fullscreenButton_ = this.createButton_('fullscreen', fullScreenToggle); |
| 154 } |
151 }; | 155 }; |
152 | 156 |
153 MediaControls.prototype.displayProgress_ = function(current, duration) { | 157 MediaControls.prototype.displayProgress_ = function(current, duration) { |
154 var ratio = current / duration; | 158 var ratio = current / duration; |
155 this.progress_.setFilled(ratio); | 159 this.progress_.setFilled(ratio); |
156 this.progress_.setValue(ratio); | 160 this.progress_.setValue(ratio); |
157 this.currentTime_.textContent = MediaControls.formatTime_(current); | 161 this.currentTime_.textContent = MediaControls.formatTime_(current); |
158 }; | 162 }; |
159 | 163 |
160 MediaControls.prototype.onPlayButtonClick_ = function() { | 164 MediaControls.prototype.onPlayButtonClick_ = function() { |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
271 this.soundButton_.classList.add('muted'); | 275 this.soundButton_.classList.add('muted'); |
272 } | 276 } |
273 }; | 277 }; |
274 | 278 |
275 MediaControls.prototype.onVolumeMouseDown_ = function () { | 279 MediaControls.prototype.onVolumeMouseDown_ = function () { |
276 if (this.media_.volume != 0) { | 280 if (this.media_.volume != 0) { |
277 this.savedVolume_ = this.media_.volume; | 281 this.savedVolume_ = this.media_.volume; |
278 } | 282 } |
279 }; | 283 }; |
280 | 284 |
281 MediaControls.prototype.onFullscreenButtonClick_ = function() { | |
282 this.fullscreenButton_.classList.toggle('on'); | |
283 // TODO: invoke the actual full screen mode | |
284 }; | |
285 | |
286 /** | 285 /** |
287 * Attach media event handlers. | 286 * Attach media event handlers. |
288 */ | 287 */ |
289 MediaControls.prototype.setupMediaEvents_ = function() { | 288 MediaControls.prototype.setupMediaEvents_ = function() { |
290 this.media_.addEventListener( | 289 this.media_.addEventListener( |
291 'play', this.onMediaPlay_.bind(this, true)); | 290 'play', this.onMediaPlay_.bind(this, true)); |
292 this.media_.addEventListener( | 291 this.media_.addEventListener( |
293 'pause', this.onMediaPlay_.bind(this, false)); | 292 'pause', this.onMediaPlay_.bind(this, false)); |
294 this.media_.addEventListener( | 293 this.media_.addEventListener( |
295 'durationchange', this.onMediaDuration_.bind(this)); | 294 'durationchange', this.onMediaDuration_.bind(this)); |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
432 /** | 431 /** |
433 * Compute the proportion in which the given position divides the slider bar. | 432 * Compute the proportion in which the given position divides the slider bar. |
434 * | 433 * |
435 * @param {number} position in pixels. | 434 * @param {number} position in pixels. |
436 * @return {number} [0..1] proportion. | 435 * @return {number} [0..1] proportion. |
437 */ | 436 */ |
438 MediaControls.Slider.prototype.getProportion = function (position) { | 437 MediaControls.Slider.prototype.getProportion = function (position) { |
439 var rect = this.bar_.getBoundingClientRect(); | 438 var rect = this.bar_.getBoundingClientRect(); |
440 return Math.max(0, Math.min(1, (position - rect.left) / rect.width)); | 439 return Math.max(0, Math.min(1, (position - rect.left) / rect.width)); |
441 }; | 440 }; |
OLD | NEW |