Source: ui/volume_bar.js

  1. /*! @license
  2. * Shaka Player
  3. * Copyright 2016 Google LLC
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. goog.provide('shaka.ui.VolumeBar');
  7. goog.require('goog.asserts');
  8. goog.require('shaka.ads.AdManager');
  9. goog.require('shaka.ui.Controls');
  10. goog.require('shaka.ui.Locales');
  11. goog.require('shaka.ui.Localization');
  12. goog.require('shaka.ui.RangeElement');
  13. /**
  14. * @extends {shaka.ui.RangeElement}
  15. * @final
  16. * @export
  17. */
  18. shaka.ui.VolumeBar = class extends shaka.ui.RangeElement {
  19. /**
  20. * @param {!HTMLElement} parent
  21. * @param {!shaka.ui.Controls} controls
  22. */
  23. constructor(parent, controls) {
  24. super(parent, controls,
  25. ['shaka-volume-bar-container'], ['shaka-volume-bar']);
  26. /** @private {!shaka.extern.UIConfiguration} */
  27. this.config_ = this.controls.getConfig();
  28. this.eventManager.listen(this.video,
  29. 'volumechange',
  30. () => this.onPresentationVolumeChange_());
  31. this.eventManager.listen(this.adManager,
  32. shaka.ads.AdManager.AD_VOLUME_CHANGED,
  33. () => this.onAdVolumeChange_());
  34. this.eventManager.listen(this.adManager,
  35. shaka.ads.AdManager.AD_MUTED,
  36. () => this.onAdVolumeChange_());
  37. this.eventManager.listen(this.adManager,
  38. shaka.ads.AdManager.AD_STOPPED,
  39. () => this.onPresentationVolumeChange_());
  40. this.eventManager.listen(this.localization,
  41. shaka.ui.Localization.LOCALE_UPDATED,
  42. () => this.updateAriaLabel_());
  43. this.eventManager.listen(this.localization,
  44. shaka.ui.Localization.LOCALE_CHANGED,
  45. () => this.updateAriaLabel_());
  46. // Initialize volume display and label.
  47. this.onPresentationVolumeChange_();
  48. this.updateAriaLabel_();
  49. if (this.ad) {
  50. // There was already an ad.
  51. this.onChange();
  52. }
  53. }
  54. /**
  55. * Update the video element's state to match the input element's state.
  56. * Called by the base class when the input element changes.
  57. *
  58. * @override
  59. */
  60. onChange() {
  61. if (this.ad) {
  62. this.ad.setVolume(this.getValue());
  63. } else {
  64. this.video.volume = this.getValue();
  65. if (this.video.volume == 0) {
  66. this.video.muted = true;
  67. } else {
  68. this.video.muted = false;
  69. }
  70. }
  71. }
  72. /** @private */
  73. onPresentationVolumeChange_() {
  74. if (this.video.muted) {
  75. this.setValue(0);
  76. } else {
  77. this.setValue(this.video.volume);
  78. }
  79. this.updateColors_();
  80. }
  81. /** @private */
  82. onAdVolumeChange_() {
  83. goog.asserts.assert(this.ad != null,
  84. 'This.ad should exist at this point!');
  85. const volume = this.ad.getVolume();
  86. this.setValue(volume);
  87. this.updateColors_();
  88. }
  89. /** @private */
  90. updateColors_() {
  91. const colors = this.config_.volumeBarColors;
  92. const gradient = ['to right'];
  93. gradient.push(colors.level + (this.getValue() * 100) + '%');
  94. gradient.push(colors.base + (this.getValue() * 100) + '%');
  95. gradient.push(colors.base + '100%');
  96. this.container.style.background =
  97. 'linear-gradient(' + gradient.join(',') + ')';
  98. }
  99. /** @private */
  100. updateAriaLabel_() {
  101. this.bar.ariaLabel = this.localization.resolve(shaka.ui.Locales.Ids.VOLUME);
  102. }
  103. };
  104. /**
  105. * @implements {shaka.extern.IUIElement.Factory}
  106. * @final
  107. */
  108. shaka.ui.VolumeBar.Factory = class {
  109. /** @override */
  110. create(rootElement, controls) {
  111. return new shaka.ui.VolumeBar(rootElement, controls);
  112. }
  113. };
  114. shaka.ui.Controls.registerElement('volume', new shaka.ui.VolumeBar.Factory());