Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

AudioMuteButton.ts 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import { connect } from 'react-redux';
  2. import { ACTION_SHORTCUT_TRIGGERED, AUDIO_MUTE, createShortcutEvent } from '../../analytics/AnalyticsEvents';
  3. import { sendAnalytics } from '../../analytics/functions';
  4. import { IReduxState } from '../../app/types';
  5. import { AUDIO_MUTE_BUTTON_ENABLED } from '../../base/flags/constants';
  6. import { getFeatureFlag } from '../../base/flags/functions';
  7. import { translate } from '../../base/i18n/functions';
  8. import { MEDIA_TYPE } from '../../base/media/constants';
  9. import AbstractAudioMuteButton from '../../base/toolbox/components/AbstractAudioMuteButton';
  10. import AbstractButton, { IProps as AbstractButtonProps } from '../../base/toolbox/components/AbstractButton';
  11. import { isLocalTrackMuted } from '../../base/tracks/functions';
  12. import { registerShortcut, unregisterShortcut } from '../../keyboard-shortcuts/actions';
  13. import { muteLocal } from '../../video-menu/actions';
  14. import { isAudioMuteButtonDisabled } from '../functions';
  15. /**
  16. * The type of the React {@code Component} props of {@link AudioMuteButton}.
  17. */
  18. interface IProps extends AbstractButtonProps {
  19. /**
  20. * Whether audio is currently muted or not.
  21. */
  22. _audioMuted: boolean;
  23. /**
  24. * Whether the button is disabled.
  25. */
  26. _disabled: boolean;
  27. }
  28. /**
  29. * Component that renders a toolbar button for toggling audio mute.
  30. *
  31. * @augments AbstractAudioMuteButton
  32. */
  33. class AudioMuteButton extends AbstractAudioMuteButton<IProps> {
  34. accessibilityLabel = 'toolbar.accessibilityLabel.mute';
  35. toggledAccessibilityLabel = 'toolbar.accessibilityLabel.unmute';
  36. label = 'toolbar.mute';
  37. tooltip = 'toolbar.mute';
  38. toggledTooltip = 'toolbar.unmute';
  39. /**
  40. * Initializes a new {@code AudioMuteButton} instance.
  41. *
  42. * @param {IProps} props - The read-only React {@code Component} props with
  43. * which the new instance is to be initialized.
  44. */
  45. constructor(props: IProps) {
  46. super(props);
  47. // Bind event handlers so they are only bound once per instance.
  48. this._onKeyboardShortcut = this._onKeyboardShortcut.bind(this);
  49. }
  50. /**
  51. * Registers the keyboard shortcut that toggles the audio muting.
  52. *
  53. * @inheritdoc
  54. * @returns {void}
  55. */
  56. componentDidMount() {
  57. if (typeof APP === 'undefined') {
  58. return;
  59. }
  60. this.props.dispatch(registerShortcut({
  61. character: 'M',
  62. helpDescription: 'keyboardShortcuts.mute',
  63. handler: this._onKeyboardShortcut
  64. }));
  65. }
  66. /**
  67. * Unregisters the keyboard shortcut that toggles the audio muting.
  68. *
  69. * @inheritdoc
  70. * @returns {void}
  71. */
  72. componentWillUnmount() {
  73. if (typeof APP === 'undefined') {
  74. return;
  75. }
  76. this.props.dispatch(unregisterShortcut('M'));
  77. }
  78. /**
  79. * Indicates if audio is currently muted or not.
  80. *
  81. * @override
  82. * @protected
  83. * @returns {boolean}
  84. */
  85. _isAudioMuted() {
  86. return this.props._audioMuted;
  87. }
  88. /**
  89. * Creates an analytics keyboard shortcut event and dispatches an action to
  90. * toggle the audio muting.
  91. *
  92. * @private
  93. * @returns {void}
  94. */
  95. _onKeyboardShortcut() {
  96. // Ignore keyboard shortcuts if the audio button is disabled.
  97. if (this._isDisabled()) {
  98. return;
  99. }
  100. sendAnalytics(
  101. createShortcutEvent(
  102. AUDIO_MUTE,
  103. ACTION_SHORTCUT_TRIGGERED,
  104. { enable: !this._isAudioMuted() }));
  105. AbstractButton.prototype._onClick.call(this);
  106. }
  107. /**
  108. * Changes the muted state.
  109. *
  110. * @param {boolean} audioMuted - Whether audio should be muted or not.
  111. * @protected
  112. * @returns {void}
  113. */
  114. _setAudioMuted(audioMuted: boolean) {
  115. this.props.dispatch(muteLocal(audioMuted, MEDIA_TYPE.AUDIO));
  116. }
  117. /**
  118. * Return a boolean value indicating if this button is disabled or not.
  119. *
  120. * @returns {boolean}
  121. */
  122. _isDisabled() {
  123. return this.props._disabled;
  124. }
  125. }
  126. /**
  127. * Maps (parts of) the redux state to the associated props for the
  128. * {@code AudioMuteButton} component.
  129. *
  130. * @param {Object} state - The Redux state.
  131. * @private
  132. * @returns {{
  133. * _audioMuted: boolean,
  134. * _disabled: boolean
  135. * }}
  136. */
  137. function _mapStateToProps(state: IReduxState) {
  138. const _audioMuted = isLocalTrackMuted(state['features/base/tracks'], MEDIA_TYPE.AUDIO);
  139. const _disabled = isAudioMuteButtonDisabled(state);
  140. const enabledFlag = getFeatureFlag(state, AUDIO_MUTE_BUTTON_ENABLED, true);
  141. return {
  142. _audioMuted,
  143. _disabled,
  144. visible: enabledFlag
  145. };
  146. }
  147. export default translate(connect(_mapStateToProps)(AudioMuteButton));