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 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* @fileoverview MediaControls class implements media playback controls
* that exist outside of the audio/video HTML element.
*/
/**
* @param {!HTMLElement} containerElement The container for the controls.
* @param {function(Event)} onMediaError Function to display an error message.
* @constructor
* @struct
*/
function MediaControls(containerElement, onMediaError) {
this.container_ = containerElement;
this.document_ = this.container_.ownerDocument;
this.media_ = null;
this.onMediaPlayBound_ = this.onMediaPlay_.bind(this, true);
this.onMediaPauseBound_ = this.onMediaPlay_.bind(this, false);
this.onMediaDurationBound_ = this.onMediaDuration_.bind(this);
this.onMediaProgressBound_ = this.onMediaProgress_.bind(this);
this.onMediaError_ = onMediaError || function() {};
this.savedVolume_ = 1; // 100% volume.
/**
* @type {HTMLElement}
* @private
*/
this.playButton_ = null;
/**
* @type {MediaControls.Slider}
* @private
*/
this.progressSlider_ = null;
/**
* @type {HTMLElement}
* @private
*/
this.duration_ = null;
/**
* @type {MediaControls.AnimatedSlider}
* @private
*/
this.volume_ = null;
/**
* @type {HTMLElement}
* @private
*/
this.textBanner_ = null;
/**
* @type {HTMLElement}
* @private
*/
this.soundButton_ = null;
/**
* @type {boolean}
* @private
*/
this.resumeAfterDrag_ = false;
/**
* @type {HTMLElement}
* @private
*/
this.currentTime_ = null;
}
/**
* Button's state types. Values are used as CSS class names.
* @enum {string}
*/
MediaControls.ButtonStateType = {
DEFAULT: 'default',
PLAYING: 'playing',
ENDED: 'ended'
};
/**
* @return {HTMLAudioElement|HTMLVideoElement} The media element.
*/
MediaControls.prototype.getMedia = function() { return this.media_ };
/**
* Format the time in hh:mm:ss format (omitting redundant leading zeros).
*
* @param {number} timeInSec Time in seconds.
* @return {string} Formatted time string.
* @private
*/
MediaControls.formatTime_ = function(timeInSec) {
var seconds = Math.floor(timeInSec % 60);
var minutes = Math.floor((timeInSec / 60) % 60);
var hours = Math.floor(timeInSec / 60 / 60);
var result = '';
if (hours) result += hours + ':';
if (hours && (minutes < 10)) result += '0';
result += minutes + ':';
if (seconds < 10) result += '0';
result += seconds;
return result;
};
/**
* Create a custom control.
*
* @param {string} className Class name.
* @param {HTMLElement=} opt_parent Parent element or container if undefined.
* @return {!HTMLElement} The new control element.
*/
MediaControls.prototype.createControl = function(className, opt_parent) {
var parent = opt_parent || this.container_;
var control = assertInstanceof(this.document_.createElement('div'),
HTMLDivElement);
control.className = className;
parent.appendChild(control);
return control;
};
/**
* Create a custom button.
*
* @param {string} className Class name.
* @param {function(Event)=} opt_handler Click handler.
* @param {HTMLElement=} opt_parent Parent element or container if undefined.
* @param {number=} opt_numStates Number of states, default: 1.
* @return {!HTMLElement} The new button element.
*/
MediaControls.prototype.createButton = function(
className, opt_handler, opt_parent, opt_numStates) {
opt_numStates = opt_numStates || 1;
var button = this.createControl(className, opt_parent);
button.classList.add('media-button');
var stateTypes = Object.keys(MediaControls.ButtonStateType);
for (var state = 0; state != opt_numStates; state++) {
var stateClass = MediaControls.ButtonStateType[stateTypes[state]];
this.createControl('normal ' + stateClass, button);
this.createControl('hover ' + stateClass, button);
this.createControl('active ' + stateClass, button);
}
this.createControl('disabled', button);
button.setAttribute('state', MediaControls.ButtonStateType.DEFAULT);
if (opt_handler)
button.addEventListener('click', opt_handler);
return button;
};
/**
* Enable/disable controls matching a given selector.
*
* @param {string} selector CSS selector.
* @param {boolean} on True if enable, false if disable.
* @private
*/
MediaControls.prototype.enableControls_ = function(selector, on) {
var controls = this.container_.querySelectorAll(selector);
for (var i = 0; i != controls.length; i++) {
var classList = controls[i].classList;
if (on)
classList.remove('disabled');
else
classList.add('disabled');
}
};
/*
* Playback control.
*/
/**
* Play the media.
*/
MediaControls.prototype.play = function() {
if (!this.media_)
return; // Media is detached.
this.media_.play();
};
/**
* Pause the media.
*/
MediaControls.prototype.pause = function() {
if (!this.media_)
return; // Media is detached.
this.media_.pause();
};
/**
* @return {boolean} True if the media is currently playing.
*/
MediaControls.prototype.isPlaying = function() {
return !!this.media_ && !this.media_.paused && !this.media_.ended;
};
/**
* Toggle play/pause.
*/
MediaControls.prototype.togglePlayState = function() {
if (this.isPlaying())
this.pause();
else
this.play();
};
/**
* Toggles play/pause state on a mouse click on the play/pause button.
*
* @param {Event} event Mouse click event.
*/
MediaControls.prototype.onPlayButtonClicked = function(event) {
this.togglePlayState();
};
/**
* @param {HTMLElement=} opt_parent Parent container.
*/
MediaControls.prototype.initPlayButton = function(opt_parent) {
this.playButton_ = this.createButton('play media-control',
this.onPlayButtonClicked.bind(this), opt_parent, 3 /* States. */);
};
/*
* Time controls
*/
/**
* The default range of 100 is too coarse for the media progress slider.
*/
MediaControls.PROGRESS_RANGE = 5000;
/**
* @param {boolean=} opt_seekMark True if the progress slider should have
* a seek mark.
* @param {HTMLElement=} opt_parent Parent container.
*/
MediaControls.prototype.initTimeControls = function(opt_seekMark, opt_parent) {
var timeControls = this.createControl('time-controls', opt_parent);
var sliderConstructor =
opt_seekMark ? MediaControls.PreciseSlider : MediaControls.Slider;
this.progressSlider_ = new sliderConstructor(
this.createControl('progress media-control', timeControls),
0, /* value */
MediaControls.PROGRESS_RANGE,
this.onProgressChange_.bind(this),
this.onProgressDrag_.bind(this));
var timeBox = this.createControl('time media-control', timeControls);
this.duration_ = this.createControl('duration', timeBox);
// Set the initial width to the minimum to reduce the flicker.
this.duration_.textContent = MediaControls.formatTime_(0);
this.currentTime_ = this.createControl('current', timeBox);
};
/**
* @param {number} current Current time is seconds.
* @param {number} duration Duration in seconds.
* @private
*/
MediaControls.prototype.displayProgress_ = function(current, duration) {
var ratio = current / duration;
this.progressSlider_.setValue(ratio);
this.currentTime_.textContent = MediaControls.formatTime_(current);
};
/**
* @param {number} value Progress [0..1].
* @private
*/
MediaControls.prototype.onProgressChange_ = function(value) {
if (!this.media_)
return; // Media is detached.
if (!this.media_.seekable || !this.media_.duration) {
console.error('Inconsistent media state');
return;
}
var current = this.media_.duration * value;
this.media_.currentTime = current;
this.currentTime_.textContent = MediaControls.formatTime_(current);
};
/**
* @param {boolean} on True if dragging.
* @private
*/
MediaControls.prototype.onProgressDrag_ = function(on) {
if (!this.media_)
return; // Media is detached.
if (on) {
this.resumeAfterDrag_ = this.isPlaying();
this.media_.pause(true /* seeking */);
} else {
if (this.resumeAfterDrag_) {
if (this.media_.ended)
this.onMediaPlay_(false);
else
this.media_.play(true /* seeking */);
}
this.updatePlayButtonState_(this.isPlaying());
}
};
/*
* Volume controls
*/
/**
* @param {HTMLElement=} opt_parent Parent element for the controls.
*/
MediaControls.prototype.initVolumeControls = function(opt_parent) {
var volumeControls = this.createControl('volume-controls', opt_parent);
this.soundButton_ = this.createButton('sound media-control',
this.onSoundButtonClick_.bind(this), volumeControls);
this.soundButton_.setAttribute('level', 3); // max level.
this.volume_ = new MediaControls.AnimatedSlider(
this.createControl('volume media-control', volumeControls),
1, /* value */
100 /* range */,
this.onVolumeChange_.bind(this),
this.onVolumeDrag_.bind(this));
};
/**
* Click handler for the sound level button.
* @private
*/
MediaControls.prototype.onSoundButtonClick_ = function() {
if (this.media_.volume == 0) {
this.volume_.setValue(this.savedVolume_ || 1);
} else {
this.savedVolume_ = this.media_.volume;
this.volume_.setValue(0);
}
this.onVolumeChange_(this.volume_.getValue());
};
/**
* @param {number} value Volume [0..1].
* @return {number} The rough level [0..3] used to pick an icon.
* @private
*/
MediaControls.getVolumeLevel_ = function(value) {
if (value == 0) return 0;
if (value <= 1 / 3) return 1;
if (value <= 2 / 3) return 2;
return 3;
};
/**
* @param {number} value Volume [0..1].
* @private
*/
MediaControls.prototype.onVolumeChange_ = function(value) {
if (!this.media_)
return; // Media is detached.
this.media_.volume = value;
this.soundButton_.setAttribute('level', MediaControls.getVolumeLevel_(value));
};
/**
* @param {boolean} on True if dragging is in progress.
* @private
*/
MediaControls.prototype.onVolumeDrag_ = function(on) {
if (on && (this.media_.volume != 0)) {
this.savedVolume_ = this.media_.volume;
}
};
/*
* Media event handlers.
*/
/**
* Attach a media element.
*
* @param {!HTMLMediaElement} mediaElement The media element to control.
*/
MediaControls.prototype.attachMedia = function(mediaElement) {
this.media_ = mediaElement;
this.media_.addEventListener('play', this.onMediaPlayBound_);
this.media_.addEventListener('pause', this.onMediaPauseBound_);
this.media_.addEventListener('durationchange', this.onMediaDurationBound_);
this.media_.addEventListener('timeupdate', this.onMediaProgressBound_);
this.media_.addEventListener('error', this.onMediaError_);
// If the text banner is being displayed, hide it immediately, since it is
// related to the previous media.
this.textBanner_.removeAttribute('visible');
// Reflect the media state in the UI.
this.onMediaDuration_();
this.onMediaPlay_(this.isPlaying());
this.onMediaProgress_();
if (this.volume_) {
/* Copy the user selected volume to the new media element. */
this.savedVolume_ = this.media_.volume = this.volume_.getValue();
}
};
/**
* Detach media event handlers.
*/
MediaControls.prototype.detachMedia = function() {
if (!this.media_)
return;
this.media_.removeEventListener('play', this.onMediaPlayBound_);
this.media_.removeEventListener('pause', this.onMediaPauseBound_);
this.media_.removeEventListener('durationchange', this.onMediaDurationBound_);
this.media_.removeEventListener('timeupdate', this.onMediaProgressBound_);
this.media_.removeEventListener('error', this.onMediaError_);
this.media_ = null;
};
/**
* Force-empty the media pipeline. This is a workaround for crbug.com/149957.
* The document is not going to be GC-ed until the last Files app window closes,
* but we want the media pipeline to deinitialize ASAP to minimize leakage.
*/
MediaControls.prototype.cleanup = function() {
if (!this.media_)
return;
this.media_.src = '';
this.media_.load();
this.detachMedia();
};
/**
* 'play' and 'pause' event handler.
* @param {boolean} playing True if playing.
* @private
*/
MediaControls.prototype.onMediaPlay_ = function(playing) {
if (this.progressSlider_.isDragging())
return;
this.updatePlayButtonState_(playing);
this.onPlayStateChanged();
};
/**
* 'durationchange' event handler.
* @private
*/
MediaControls.prototype.onMediaDuration_ = function() {
if (!this.media_ || !this.media_.duration) {
this.enableControls_('.media-control', false);
return;
}
this.enableControls_('.media-control', true);
var sliderContainer = this.progressSlider_.getContainer();
if (this.media_.seekable)
sliderContainer.classList.remove('readonly');
else
sliderContainer.classList.add('readonly');
var valueToString = function(value) {
var duration = this.media_ ? this.media_.duration : 0;
return MediaControls.formatTime_(this.media_.duration * value);
}.bind(this);
this.duration_.textContent = valueToString(1);
this.progressSlider_.setValueToStringFunction(valueToString);
if (this.media_.seekable)
this.restorePlayState();
};
/**
* 'timeupdate' event handler.
* @private
*/
MediaControls.prototype.onMediaProgress_ = function() {
if (!this.media_ || !this.media_.duration) {
this.displayProgress_(0, 1);
return;
}
var current = this.media_.currentTime;
var duration = this.media_.duration;
if (this.progressSlider_.isDragging())
return;
this.displayProgress_(current, duration);
if (current == duration) {
this.onMediaComplete();
}
this.onPlayStateChanged();
};
/**
* Called when the media playback is complete.
*/
MediaControls.prototype.onMediaComplete = function() {};
/**
* Called when play/pause state is changed or on playback progress.
* This is the right moment to save the play state.
*/
MediaControls.prototype.onPlayStateChanged = function() {};
/**
* Updates the play button state.
* @param {boolean} playing If the video is playing.
* @private
*/
MediaControls.prototype.updatePlayButtonState_ = function(playing) {
if (this.media_.ended && this.progressSlider_.isAtEnd()) {
this.playButton_.setAttribute('state',
MediaControls.ButtonStateType.ENDED);
} else if (playing) {
this.playButton_.setAttribute('state',
MediaControls.ButtonStateType.PLAYING);
} else {
this.playButton_.setAttribute('state',
MediaControls.ButtonStateType.DEFAULT);
}
};
/**
* Restore play state. Base implementation is empty.
*/
MediaControls.prototype.restorePlayState = function() {};
/**
* Encode current state into the page URL or the app state.
*/
MediaControls.prototype.encodeState = function() {
if (!this.media_ || !this.media_.duration)
return;
if (window.appState) {
window.appState.time = this.media_.currentTime;
util.saveAppState();
}
return;
};
/**
* Decode current state from the page URL or the app state.
* @return {boolean} True if decode succeeded.
*/
MediaControls.prototype.decodeState = function() {
if (!this.media_ || !window.appState || !('time' in window.appState))
return false;
// There is no page reload for apps v2, only app restart.
// Always restart in paused state.
this.media_.currentTime = window.appState.time;
this.pause();
return true;
};
/**
* Remove current state from the page URL or the app state.
*/
MediaControls.prototype.clearState = function() {
if (!window.appState)
return;
if ('time' in window.appState)
delete window.appState.time;
util.saveAppState();
return;
};
/**
* Create a customized slider control.
*
* @param {!HTMLElement} container The containing div element.
* @param {number} value Initial value [0..1].
* @param {number} range Number of distinct slider positions to be supported.
* @param {function(number)} onChange Value change handler.
* @param {function(boolean)} onDrag Drag begin/end handler.
* @constructor
* @struct
*/
MediaControls.Slider = function(container, value, range, onChange, onDrag) {
this.container_ = container;
this.onChange_ = onChange;
this.onDrag_ = onDrag;
/**
* @type {boolean}
* @private
*/
this.isDragging_ = false;
var document = this.container_.ownerDocument;
this.container_.classList.add('custom-slider');
this.input_ = assertInstanceof(document.createElement('input'),
HTMLInputElement);
this.input_.type = 'range';
this.input_.min = (0).toString();
this.input_.max = range.toString();
this.input_.value = (value * range).toString();
this.container_.appendChild(this.input_);
this.input_.addEventListener(
'change', this.onInputChange_.bind(this));
this.input_.addEventListener(
'mousedown', this.onInputDrag_.bind(this, true));
this.input_.addEventListener(
'mouseup', this.onInputDrag_.bind(this, false));
this.bar_ = assertInstanceof(document.createElement('div'), HTMLDivElement);
this.bar_.className = 'bar';
this.container_.appendChild(this.bar_);
this.filled_ = document.createElement('div');
this.filled_.className = 'filled';
this.bar_.appendChild(this.filled_);
var leftCap = document.createElement('div');
leftCap.className = 'cap left';
this.bar_.appendChild(leftCap);
var rightCap = document.createElement('div');
rightCap.className = 'cap right';
this.bar_.appendChild(rightCap);
this.value_ = value;
this.setFilled_(value);
};
/**
* @return {HTMLElement} The container element.
*/
MediaControls.Slider.prototype.getContainer = function() {
return this.container_;
};
/**
* @return {HTMLElement} The standard input element.
* @private
*/
MediaControls.Slider.prototype.getInput_ = function() {
return this.input_;
};
/**
* @return {HTMLElement} The slider bar element.
*/
MediaControls.Slider.prototype.getBar = function() {
return this.bar_;
};
/**
* @return {number} [0..1] The current value.
*/
MediaControls.Slider.prototype.getValue = function() {
return this.value_;
};
/**
* @param {number} value [0..1].
*/
MediaControls.Slider.prototype.setValue = function(value) {
this.value_ = value;
this.setValueToUI_(value);
};
/**
* Fill the given proportion the slider bar (from the left).
*
* @param {number} proportion [0..1].
* @private
*/
MediaControls.Slider.prototype.setFilled_ = function(proportion) {
this.filled_.style.width = proportion * 100 + '%';
};
/**
* Get the value from the input element.
*
* @return {number} Value [0..1].
* @private
*/
MediaControls.Slider.prototype.getValueFromUI_ = function() {
return this.input_.value / this.input_.max;
};
/**
* Update the UI with the current value.
*
* @param {number} value [0..1].
* @private
*/
MediaControls.Slider.prototype.setValueToUI_ = function(value) {
this.input_.value = (value * this.input_.max).toString();
this.setFilled_(value);
};
/**
* Compute the proportion in which the given position divides the slider bar.
*
* @param {number} position in pixels.
* @return {number} [0..1] proportion.
*/
MediaControls.Slider.prototype.getProportion = function(position) {
var rect = this.bar_.getBoundingClientRect();
return Math.max(0, Math.min(1, (position - rect.left) / rect.width));
};
/**
* Sets value formatting function.
* @param {function(number):string} func Value formatting function.
*/
MediaControls.Slider.prototype.setValueToStringFunction = function(func) {};
/**
* 'change' event handler.
* @private
*/
MediaControls.Slider.prototype.onInputChange_ = function() {
this.value_ = this.getValueFromUI_();
this.setFilled_(this.value_);
this.onChange_(this.value_);
};
/**
* @return {boolean} True if dragging is in progress.
*/
MediaControls.Slider.prototype.isDragging = function() {
return this.isDragging_;
};
/**
* Mousedown/mouseup handler.
* @param {boolean} on True if the mouse is down.
* @private
*/
MediaControls.Slider.prototype.onInputDrag_ = function(on) {
this.isDragging_ = on;
this.onDrag_(on);
};
/**
* Check if the slider position is at the end of the control.
* @return {boolean} True if the slider position is at the end.
*/
MediaControls.Slider.prototype.isAtEnd = function() {
return this.input_.value === this.input_.max;
};
/**
* Create a customized slider with animated thumb movement.
*
* @param {!HTMLElement} container The containing div element.
* @param {number} value Initial value [0..1].
* @param {number} range Number of distinct slider positions to be supported.
* @param {function(number)} onChange Value change handler.
* @param {function(boolean)} onDrag Drag begin/end handler.
* @constructor
* @struct
* @extends {MediaControls.Slider}
*/
MediaControls.AnimatedSlider = function(
container, value, range, onChange, onDrag) {
MediaControls.Slider.apply(this, arguments);
/**
* @type {number}
* @private
*/
this.animationInterval_ = 0;
};
MediaControls.AnimatedSlider.prototype = {
__proto__: MediaControls.Slider.prototype
};
/**
* Number of animation steps.
*/
MediaControls.AnimatedSlider.STEPS = 10;
/**
* Animation duration.
*/
MediaControls.AnimatedSlider.DURATION = 100;
/**
* @param {number} value [0..1].
* @private
*/
MediaControls.AnimatedSlider.prototype.setValueToUI_ = function(value) {
if (this.animationInterval_) {
clearInterval(this.animationInterval_);
}
var oldValue = this.getValueFromUI_();
var step = 0;
this.animationInterval_ = setInterval(function() {
step++;
var currentValue = oldValue +
(value - oldValue) * (step / MediaControls.AnimatedSlider.STEPS);
MediaControls.Slider.prototype.setValueToUI_.call(this, currentValue);
if (step == MediaControls.AnimatedSlider.STEPS) {
clearInterval(this.animationInterval_);
}
}.bind(this),
MediaControls.AnimatedSlider.DURATION / MediaControls.AnimatedSlider.STEPS);
};
/**
* Create a customized slider with a precise time feedback.
*
* The time value is shown above the slider bar at the mouse position.
*
* @param {!HTMLElement} container The containing div element.
* @param {number} value Initial value [0..1].
* @param {number} range Number of distinct slider positions to be supported.
* @param {function(number)} onChange Value change handler.
* @param {function(boolean)} onDrag Drag begin/end handler.
* @param {function(number):string} formatFunction Value formatting function.
* @constructor
* @struct
* @extends {MediaControls.Slider}
*/
MediaControls.PreciseSlider = function(
container, value, range, onChange, onDrag, formatFunction) {
MediaControls.Slider.apply(this, arguments);
var doc = this.container_.ownerDocument;
/**
* @type {number}
* @private
*/
this.latestMouseUpTime_ = 0;
/**
* @type {number}
* @private
*/
this.seekMarkTimer_ = 0;
/**
* @type {number}
* @private
*/
this.latestSeekRatio_ = 0;
/**
* @type {?function(number):string}
* @private
*/
this.valueToString_ = null;
this.seekMark_ = doc.createElement('div');
this.seekMark_.className = 'seek-mark';
this.getBar().appendChild(this.seekMark_);
this.seekLabel_ = doc.createElement('div');
this.seekLabel_.className = 'seek-label';
this.seekMark_.appendChild(this.seekLabel_);
this.getContainer().addEventListener(
'mousemove', this.onMouseMove_.bind(this));
this.getContainer().addEventListener(
'mouseout', this.onMouseOut_.bind(this));
};
MediaControls.PreciseSlider.prototype = {
__proto__: MediaControls.Slider.prototype
};
/**
* Show the seek mark after a delay.
*/
MediaControls.PreciseSlider.SHOW_DELAY = 200;
/**
* Hide the seek mark for this long after changing the position with a click.
*/
MediaControls.PreciseSlider.HIDE_AFTER_MOVE_DELAY = 2500;
/**
* Hide the seek mark for this long after changing the position with a drag.
*/
MediaControls.PreciseSlider.HIDE_AFTER_DRAG_DELAY = 750;
/**
* Default hide timeout (no hiding).
*/
MediaControls.PreciseSlider.NO_AUTO_HIDE = 0;
/**
* @override
*/
MediaControls.PreciseSlider.prototype.setValueToStringFunction =
function(func) {
this.valueToString_ = func;
/* It is not completely accurate to assume that the max value corresponds
to the longest string, but generous CSS padding will compensate for that. */
var labelWidth = this.valueToString_(1).length / 2 + 1;
this.seekLabel_.style.width = labelWidth + 'em';
this.seekLabel_.style.marginLeft = -labelWidth / 2 + 'em';
};
/**
* Show the time above the slider.
*
* @param {number} ratio [0..1] The proportion of the duration.
* @param {number} timeout Timeout in ms after which the label should be hidden.
* MediaControls.PreciseSlider.NO_AUTO_HIDE means show until the next call.
* @private
*/
MediaControls.PreciseSlider.prototype.showSeekMark_ =
function(ratio, timeout) {
// Do not update the seek mark for the first 500ms after the drag is finished.
if (this.latestMouseUpTime_ && (this.latestMouseUpTime_ + 500 > Date.now()))
return;
this.seekMark_.style.left = ratio * 100 + '%';
if (ratio < this.getValue()) {
this.seekMark_.classList.remove('inverted');
} else {
this.seekMark_.classList.add('inverted');
}
this.seekLabel_.textContent = this.valueToString_(ratio);
this.seekMark_.classList.add('visible');
if (this.seekMarkTimer_) {
clearTimeout(this.seekMarkTimer_);
this.seekMarkTimer_ = 0;
}
if (timeout != MediaControls.PreciseSlider.NO_AUTO_HIDE) {
this.seekMarkTimer_ = setTimeout(this.hideSeekMark_.bind(this), timeout);
}
};
/**
* @private
*/
MediaControls.PreciseSlider.prototype.hideSeekMark_ = function() {
this.seekMarkTimer_ = 0;
this.seekMark_.classList.remove('visible');
};
/**
* 'mouseout' event handler.
* @param {Event} e Event.
* @private
*/
MediaControls.PreciseSlider.prototype.onMouseMove_ = function(e) {
this.latestSeekRatio_ = this.getProportion(e.clientX);
var showMark = function() {
if (!this.isDragging()) {
this.showSeekMark_(this.latestSeekRatio_,
MediaControls.PreciseSlider.HIDE_AFTER_MOVE_DELAY);
}
}.bind(this);
if (this.seekMark_.classList.contains('visible')) {
showMark();
} else if (!this.seekMarkTimer_) {
this.seekMarkTimer_ =
setTimeout(showMark, MediaControls.PreciseSlider.SHOW_DELAY);
}
};
/**
* 'mouseout' event handler.
* @param {Event} e Event.
* @private
*/
MediaControls.PreciseSlider.prototype.onMouseOut_ = function(e) {
for (var element = e.relatedTarget; element; element = element.parentNode) {
if (element == this.getContainer())
return;
}
if (this.seekMarkTimer_) {
clearTimeout(this.seekMarkTimer_);
this.seekMarkTimer_ = 0;
}
this.hideSeekMark_();
};
/**
* 'change' event handler.
* @private
*/
MediaControls.PreciseSlider.prototype.onInputChange_ = function() {
MediaControls.Slider.prototype.onInputChange_.apply(this, arguments);
if (this.isDragging()) {
this.showSeekMark_(
this.getValue(), MediaControls.PreciseSlider.NO_AUTO_HIDE);
}
};
/**
* Mousedown/mouseup handler.
* @param {boolean} on True if the mouse is down.
* @private
*/
MediaControls.PreciseSlider.prototype.onInputDrag_ = function(on) {
MediaControls.Slider.prototype.onInputDrag_.apply(this, arguments);
if (on) {
// Dragging started, align the seek mark with the thumb position.
this.showSeekMark_(
this.getValue(), MediaControls.PreciseSlider.NO_AUTO_HIDE);
} else {
// Just finished dragging.
// Show the label for the last time with a shorter timeout.
this.showSeekMark_(
this.getValue(), MediaControls.PreciseSlider.HIDE_AFTER_DRAG_DELAY);
this.latestMouseUpTime_ = Date.now();
}
};
/**
* Create video controls.
*
* @param {!HTMLElement} containerElement The container for the controls.
* @param {function(Event)} onMediaError Function to display an error message.
* @param {function(string):string} stringFunction Function providing localized
* strings.
* @param {function(Event)=} opt_fullScreenToggle Function to toggle fullscreen
* mode.
* @param {HTMLElement=} opt_stateIconParent The parent for the icon that
* gives visual feedback when the playback state changes.
* @constructor
* @struct
* @extends {MediaControls}
*/
function VideoControls(containerElement, onMediaError, stringFunction,
opt_fullScreenToggle, opt_stateIconParent) {
MediaControls.call(this, containerElement, onMediaError);
this.stringFunction_ = stringFunction;
this.container_.classList.add('video-controls');
this.initPlayButton();
this.initTimeControls(true /* show seek mark */);
this.initVolumeControls();
// Create the cast button.
this.castButton_ = this.createButton('cast menubutton');
this.castButton_.setAttribute('menu', '#cast-menu');
this.castButton_.setAttribute(
'label', this.stringFunction_('VIDEO_PLAYER_PLAY_ON'));
cr.ui.decorate(this.castButton_, cr.ui.MenuButton);
if (opt_fullScreenToggle) {
this.fullscreenButton_ =
this.createButton('fullscreen', opt_fullScreenToggle);
}
if (opt_stateIconParent) {
this.stateIcon_ = this.createControl(
'playback-state-icon', opt_stateIconParent);
this.textBanner_ = this.createControl('text-banner', opt_stateIconParent);
}
// Disables all controls at first.
this.enableControls_('.media-control', false);
var videoControls = this;
chrome.mediaPlayerPrivate.onTogglePlayState.addListener(
function() { videoControls.togglePlayStateWithFeedback(); });
}
/**
* No resume if we are within this margin from the start or the end.
*/
VideoControls.RESUME_MARGIN = 0.03;
/**
* No resume for videos shorter than this.
*/
VideoControls.RESUME_THRESHOLD = 5 * 60; // 5 min.
/**
* When resuming rewind back this much.
*/
VideoControls.RESUME_REWIND = 5; // seconds.
VideoControls.prototype = { __proto__: MediaControls.prototype };
/**
* Shows icon feedback for the current state of the video player.
* @private
*/
VideoControls.prototype.showIconFeedback_ = function() {
var stateIcon = this.stateIcon_;
stateIcon.removeAttribute('state');
setTimeout(function() {
var newState = this.isPlaying() ? 'play' : 'pause';
var onAnimationEnd = function(state, event) {
if (stateIcon.getAttribute('state') === state)
stateIcon.removeAttribute('state');
stateIcon.removeEventListener('webkitAnimationEnd', onAnimationEnd);
}.bind(null, newState);
stateIcon.addEventListener('webkitAnimationEnd', onAnimationEnd);
// Shows the icon with animation.
stateIcon.setAttribute('state', newState);
}.bind(this), 0);
};
/**
* Shows a text banner.
*
* @param {string} identifier String identifier.
* @private
*/
VideoControls.prototype.showTextBanner_ = function(identifier) {
this.textBanner_.removeAttribute('visible');
this.textBanner_.textContent = this.stringFunction_(identifier);
setTimeout(function() {
var onAnimationEnd = function(event) {
this.textBanner_.removeEventListener(
'webkitAnimationEnd', onAnimationEnd);
this.textBanner_.removeAttribute('visible');
}.bind(this);
this.textBanner_.addEventListener('webkitAnimationEnd', onAnimationEnd);
this.textBanner_.setAttribute('visible', 'true');
}.bind(this), 0);
};
/**
* @override
*/
VideoControls.prototype.onPlayButtonClicked = function(event) {
if (event.ctrlKey) {
this.toggleLoopedModeWithFeedback(true);
if (!this.isPlaying())
this.togglePlayState();
} else {
this.togglePlayState();
}
};
/**
* Media completion handler.
*/
VideoControls.prototype.onMediaComplete = function() {
this.onMediaPlay_(false); // Just update the UI.
this.savePosition(); // This will effectively forget the position.
};
/**
* Toggles the looped mode with feedback.
* @param {boolean} on Whether enabled or not.
*/
VideoControls.prototype.toggleLoopedModeWithFeedback = function(on) {
if (!this.getMedia().duration)
return;
this.toggleLoopedMode(on);
if (on) {
// TODO(mtomasz): Simplify, crbug.com/254318.
this.showTextBanner_('VIDEO_PLAYER_LOOPED_MODE');
}
};
/**
* Toggles the looped mode.
* @param {boolean} on Whether enabled or not.
*/
VideoControls.prototype.toggleLoopedMode = function(on) {
this.getMedia().loop = on;
};
/**
* Toggles play/pause state and flash an icon over the video.
*/
VideoControls.prototype.togglePlayStateWithFeedback = function() {
if (!this.getMedia().duration)
return;
this.togglePlayState();
this.showIconFeedback_();
};
/**
* Toggles play/pause state.
*/
VideoControls.prototype.togglePlayState = function() {
if (this.isPlaying()) {
// User gave the Pause command. Save the state and reset the loop mode.
this.toggleLoopedMode(false);
this.savePosition();
}
MediaControls.prototype.togglePlayState.apply(this, arguments);
};
/**
* Saves the playback position to the persistent storage.
* @param {boolean=} opt_sync True if the position must be saved synchronously
* (required when closing app windows).
*/
VideoControls.prototype.savePosition = function(opt_sync) {
if (!this.media_ ||
!this.media_.duration ||
this.media_.duration < VideoControls.RESUME_THRESHOLD) {
return;
}
var ratio = this.media_.currentTime / this.media_.duration;
var position;
if (ratio < VideoControls.RESUME_MARGIN ||
ratio > (1 - VideoControls.RESUME_MARGIN)) {
// We are too close to the beginning or the end.
// Remove the resume position so that next time we start from the beginning.
position = null;
} else {
position = Math.floor(
Math.max(0, this.media_.currentTime - VideoControls.RESUME_REWIND));
}
if (opt_sync) {
// Packaged apps cannot save synchronously.
// Pass the data to the background page.
if (!window.saveOnExit)
window.saveOnExit = [];
window.saveOnExit.push({ key: this.media_.src, value: position });
} else {
util.AppCache.update(this.media_.src, position);
}
};
/**
* Resumes the playback position saved in the persistent storage.
*/
VideoControls.prototype.restorePlayState = function() {
if (this.media_ && this.media_.duration >= VideoControls.RESUME_THRESHOLD) {
util.AppCache.getValue(this.media_.src, function(position) {
if (position)
this.media_.currentTime = position;
}.bind(this));
}
};
/**
* Updates style to best fit the size of the container.
*/
VideoControls.prototype.updateStyle = function() {
// We assume that the video controls element fills the parent container.
// This is easier than adding margins to this.container_.clientWidth.
var width = this.container_.parentNode.clientWidth;
// Set the margin to 5px for width >= 400, 0px for width < 160,
// interpolate linearly in between.
this.container_.style.margin =
Math.ceil((Math.max(160, Math.min(width, 400)) - 160) / 48) + 'px';
var hideBelow = function(selector, limit) {
this.container_.querySelector(selector).style.display =
width < limit ? 'none' : '-webkit-box';
}.bind(this);
hideBelow('.time', 350);
hideBelow('.volume', 275);
hideBelow('.volume-controls', 210);
hideBelow('.fullscreen', 150);
};
/**
* Creates audio controls.
*
* @param {!HTMLElement} container Parent container.
* @param {function(boolean)} advanceTrack Parameter: true=forward.
* @param {function(Event)} onError Error handler.
* @constructor
* @struct
* @extends {MediaControls}
*/
function AudioControls(container, advanceTrack, onError) {
MediaControls.call(this, container, onError);
this.container_.classList.add('audio-controls');
this.advanceTrack_ = advanceTrack;
this.initPlayButton();
this.initTimeControls(false /* no seek mark */);
/* No volume controls */
this.createButton('previous', this.onAdvanceClick_.bind(this, false));
this.createButton('next', this.onAdvanceClick_.bind(this, true));
// Disables all controls at first.
this.enableControls_('.media-control', false);
var audioControls = this;
chrome.mediaPlayerPrivate.onNextTrack.addListener(
function() { audioControls.onAdvanceClick_(true); });
chrome.mediaPlayerPrivate.onPrevTrack.addListener(
function() { audioControls.onAdvanceClick_(false); });
chrome.mediaPlayerPrivate.onTogglePlayState.addListener(
function() { audioControls.togglePlayState(); });
}
AudioControls.prototype = { __proto__: MediaControls.prototype };
/**
* Media completion handler. Advances to the next track.
*/
AudioControls.prototype.onMediaComplete = function() {
this.advanceTrack_(true);
};
/**
* The track position after which "previous" button acts as "restart".
*/
AudioControls.TRACK_RESTART_THRESHOLD = 5; // seconds.
/**
* @param {boolean} forward True if advancing forward.
* @private
*/
AudioControls.prototype.onAdvanceClick_ = function(forward) {
if (!forward &&
(this.getMedia().currentTime > AudioControls.TRACK_RESTART_THRESHOLD)) {
// We are far enough from the beginning of the current track.
// Restart it instead of than skipping to the previous one.
this.getMedia().currentTime = 0;
} else {
this.advanceTrack_(forward);
}
};
|