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 'use strict'; | 5 'use strict'; |
6 | 6 |
7 /** | 7 /** |
8 * @fileoverview MediaControls class implements media playback controls | 8 * @fileoverview MediaControls class implements media playback controls |
9 * that exist outside of the audio/video HTML element. | 9 * that exist outside of the audio/video HTML element. |
10 */ | 10 */ |
(...skipping 472 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
483 */ | 483 */ |
484 MediaControls.prototype.restorePlayState = function() {}; | 484 MediaControls.prototype.restorePlayState = function() {}; |
485 | 485 |
486 /** | 486 /** |
487 * Encode current state into the page URL or the app state. | 487 * Encode current state into the page URL or the app state. |
488 */ | 488 */ |
489 MediaControls.prototype.encodeState = function() { | 489 MediaControls.prototype.encodeState = function() { |
490 if (!this.media_.duration) | 490 if (!this.media_.duration) |
491 return; | 491 return; |
492 | 492 |
493 if (window.appState) { | 493 window.appState.time = this.media_.currentTime; |
494 window.appState.time = this.media_.currentTime; | 494 util.saveAppState(); |
495 util.saveAppState(); | 495 return; |
496 return; | |
497 } | |
498 | |
499 var playState = JSON.stringify({ | |
500 play: this.isPlaying(), | |
501 time: this.media_.currentTime | |
502 }); | |
503 | |
504 var newLocation = document.location.origin + document.location.pathname + | |
505 document.location.search + '#' + playState; | |
506 | |
507 document.location.href = newLocation; | |
508 }; | 496 }; |
509 | 497 |
510 /** | 498 /** |
511 * Decode current state from the page URL or the app state. | 499 * Decode current state from the page URL or the app state. |
512 * @return {boolean} True if decode succeeded. | 500 * @return {boolean} True if decode succeeded. |
513 */ | 501 */ |
514 MediaControls.prototype.decodeState = function() { | 502 MediaControls.prototype.decodeState = function() { |
515 if (window.appState) { | 503 if (!('time' in window.appState)) |
516 if (!('time' in window.appState)) | 504 return false; |
517 return false; | 505 // There is no page reload for apps v2, only app restart. |
518 // There is no page reload for apps v2, only app restart. | 506 // Always restart in paused state. |
519 // Always restart in paused state. | 507 this.media_.currentTime = appState.time; |
520 this.media_.currentTime = appState.time; | 508 this.pause(); |
521 this.pause(); | 509 return true; |
522 return true; | |
523 } | |
524 | |
525 var hash = document.location.hash.substring(1); | |
526 if (hash) { | |
527 try { | |
528 var playState = JSON.parse(hash); | |
529 if (!('time' in playState)) | |
530 return false; | |
531 | |
532 this.media_.currentTime = playState.time; | |
533 | |
534 if (playState.play) | |
535 this.play(); | |
536 else | |
537 this.pause(); | |
538 | |
539 return true; | |
540 } catch (e) { | |
541 console.warn('Cannot decode play state'); | |
542 } | |
543 } | |
544 return false; | |
545 }; | 510 }; |
546 | 511 |
547 /** | 512 /** |
548 * Remove current state from the page URL or the app state. | 513 * Remove current state from the page URL or the app state. |
549 */ | 514 */ |
550 MediaControls.prototype.clearState = function() { | 515 MediaControls.prototype.clearState = function() { |
551 if (window.appState) { | 516 if ('time' in window.appState) |
552 if ('time' in window.appState) | 517 delete window.appState.time; |
553 delete window.appState.time; | 518 util.saveAppState(); |
554 util.saveAppState(); | 519 return; |
555 return; | |
556 } | |
557 | |
558 var newLocation = document.location.origin + document.location.pathname + | |
559 document.location.search + '#'; | |
560 | |
561 document.location.href = newLocation; | |
562 }; | 520 }; |
563 | 521 |
564 /** | 522 /** |
565 * Create a customized slider control. | 523 * Create a customized slider control. |
566 * | 524 * |
567 * @param {HTMLElement} container The containing div element. | 525 * @param {HTMLElement} container The containing div element. |
568 * @param {number} value Initial value [0..1]. | 526 * @param {number} value Initial value [0..1]. |
569 * @param {number} range Number of distinct slider positions to be supported. | 527 * @param {number} range Number of distinct slider positions to be supported. |
570 * @param {function(number)} onChange Value change handler. | 528 * @param {function(number)} onChange Value change handler. |
571 * @param {function(boolean)} onDrag Drag begin/end handler. | 529 * @param {function(boolean)} onDrag Drag begin/end handler. |
(...skipping 664 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1236 AudioControls.prototype.onAdvanceClick_ = function(forward) { | 1194 AudioControls.prototype.onAdvanceClick_ = function(forward) { |
1237 if (!forward && | 1195 if (!forward && |
1238 (this.getMedia().currentTime > AudioControls.TRACK_RESTART_THRESHOLD)) { | 1196 (this.getMedia().currentTime > AudioControls.TRACK_RESTART_THRESHOLD)) { |
1239 // We are far enough from the beginning of the current track. | 1197 // We are far enough from the beginning of the current track. |
1240 // Restart it instead of than skipping to the previous one. | 1198 // Restart it instead of than skipping to the previous one. |
1241 this.getMedia().currentTime = 0; | 1199 this.getMedia().currentTime = 0; |
1242 } else { | 1200 } else { |
1243 this.advanceTrack_(forward); | 1201 this.advanceTrack_(forward); |
1244 } | 1202 } |
1245 }; | 1203 }; |
OLD | NEW |