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.Utils');
  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. // We use a range of 100 to avoid problems with Firefox.
  29. // See https://github.com/shaka-project/shaka-player/issues/3987
  30. this.setRange(0, 100);
  31. this.eventManager.listen(this.video,
  32. 'volumechange',
  33. () => this.onPresentationVolumeChange_());
  34. this.eventManager.listen(this.player,
  35. 'loading',
  36. () => this.onPresentationVolumeChange_());
  37. this.eventManager.listen(this.controls,
  38. 'caststatuschanged',
  39. () => this.onPresentationVolumeChange_());
  40. this.eventManager.listen(this.adManager,
  41. shaka.ads.Utils.AD_VOLUME_CHANGED,
  42. () => this.onAdVolumeChange_());
  43. this.eventManager.listen(this.adManager,
  44. shaka.ads.Utils.AD_MUTED,
  45. () => this.onAdVolumeChange_());
  46. this.eventManager.listen(this.adManager,
  47. shaka.ads.Utils.AD_STOPPED,
  48. () => this.onPresentationVolumeChange_());
  49. this.eventManager.listen(this.localization,
  50. shaka.ui.Localization.LOCALE_UPDATED,
  51. () => this.updateAriaLabel_());
  52. this.eventManager.listen(this.localization,
  53. shaka.ui.Localization.LOCALE_CHANGED,
  54. () => this.updateAriaLabel_());
  55. // Initialize volume display and label.
  56. this.onPresentationVolumeChange_();
  57. this.updateAriaLabel_();
  58. if (this.ad) {
  59. // There was already an ad.
  60. this.onChange();
  61. }
  62. }
  63. /**
  64. * Update the video element's state to match the input element's state.
  65. * Called by the base class when the input element changes.
  66. *
  67. * @override
  68. */
  69. onChange() {
  70. if (this.ad && this.ad.isLinear()) {
  71. this.ad.setVolume(this.getValue() / 100);
  72. } else {
  73. this.video.volume = this.getValue() / 100;
  74. if (this.video.volume > 0) {
  75. this.video.muted = false;
  76. }
  77. }
  78. }
  79. /** @private */
  80. onPresentationVolumeChange_() {
  81. if (this.video.muted) {
  82. this.setValue(0);
  83. } else {
  84. this.setValue(this.video.volume * 100);
  85. }
  86. this.updateColors_();
  87. }
  88. /** @private */
  89. onAdVolumeChange_() {
  90. goog.asserts.assert(this.ad != null,
  91. 'This.ad should exist at this point!');
  92. const volume = this.ad.getVolume();
  93. this.setValue(volume * 100);
  94. this.updateColors_();
  95. }
  96. /** @private */
  97. updateColors_() {
  98. const colors = this.config_.volumeBarColors;
  99. const gradient = ['to right'];
  100. gradient.push(colors.level + this.getValue() + '%');
  101. gradient.push(colors.base + this.getValue() + '%');
  102. gradient.push(colors.base + '100%');
  103. this.container.style.background =
  104. 'linear-gradient(' + gradient.join(',') + ')';
  105. }
  106. /** @private */
  107. updateAriaLabel_() {
  108. this.bar.ariaLabel = this.localization.resolve(shaka.ui.Locales.Ids.VOLUME);
  109. }
  110. };
  111. /**
  112. * @implements {shaka.extern.IUIElement.Factory}
  113. * @final
  114. */
  115. shaka.ui.VolumeBar.Factory = class {
  116. /** @override */
  117. create(rootElement, controls) {
  118. return new shaka.ui.VolumeBar(rootElement, controls);
  119. }
  120. };
  121. shaka.ui.Controls.registerElement('volume', new shaka.ui.VolumeBar.Factory());