您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

AudioMuteButton.js 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // @flow
  2. import { connect } from 'react-redux';
  3. import {
  4. ACTION_SHORTCUT_TRIGGERED,
  5. AUDIO_MUTE,
  6. createShortcutEvent,
  7. createToolbarEvent,
  8. sendAnalytics
  9. } from '../../analytics';
  10. import { translate } from '../../base/i18n';
  11. import { MEDIA_TYPE, setAudioMuted } from '../../base/media';
  12. import { AbstractAudioMuteButton } from '../../base/toolbox';
  13. import type { AbstractButtonProps } from '../../base/toolbox';
  14. import { isLocalTrackMuted } from '../../base/tracks';
  15. import UIEvents from '../../../../service/UI/UIEvents';
  16. declare var APP: Object;
  17. /**
  18. * The type of the React {@code Component} props of {@link AudioMuteButton}.
  19. */
  20. type Props = AbstractButtonProps & {
  21. /**
  22. * Whether audio is currently muted or not.
  23. */
  24. _audioMuted: boolean,
  25. /**
  26. * The redux {@code dispatch} function.
  27. */
  28. dispatch: Function
  29. }
  30. /**
  31. * Component that renders a toolbar button for toggling audio mute.
  32. *
  33. * @extends AbstractAudioMuteButton
  34. */
  35. class AudioMuteButton extends AbstractAudioMuteButton<Props, *> {
  36. label = 'toolbar.mute';
  37. tooltip = 'toolbar.mute';
  38. /**
  39. * Initializes a new {@code AudioMuteButton} instance.
  40. *
  41. * @param {Props} props - The read-only React {@code Component} props with
  42. * which the new instance is to be initialized.
  43. */
  44. constructor(props: Props) {
  45. super(props);
  46. // Bind event handlers so they are only bound once per instance.
  47. this._onKeyboardShortcut = this._onKeyboardShortcut.bind(this);
  48. }
  49. /**
  50. * Registers the keyboard shortcut that toggles the audio muting.
  51. *
  52. * @inheritdoc
  53. * @returns {void}
  54. */
  55. componentDidMount() {
  56. typeof APP === 'undefined'
  57. || APP.keyboardshortcut.registerShortcut(
  58. 'M',
  59. null,
  60. this._onKeyboardShortcut,
  61. 'keyboardShortcuts.mute');
  62. }
  63. /**
  64. * Unregisters the keyboard shortcut that toggles the audio muting.
  65. *
  66. * @inheritdoc
  67. * @returns {void}
  68. */
  69. componentWillUnmount() {
  70. typeof APP === 'undefined'
  71. || APP.keyboardshortcut.unregisterShortcut('M');
  72. }
  73. /**
  74. * Indicates if audio is currently muted ot nor.
  75. *
  76. * @override
  77. * @protected
  78. * @returns {boolean}
  79. */
  80. _isAudioMuted() {
  81. return this.props._audioMuted;
  82. }
  83. _onKeyboardShortcut: () => void;
  84. /**
  85. * Creates an analytics keyboard shortcut event and dispatches an action to
  86. * toggle the audio muting.
  87. *
  88. * @private
  89. * @returns {void}
  90. */
  91. _onKeyboardShortcut() {
  92. sendAnalytics(
  93. createShortcutEvent(
  94. AUDIO_MUTE,
  95. ACTION_SHORTCUT_TRIGGERED,
  96. { enable: !this._isAudioMuted() }));
  97. super._handleClick();
  98. }
  99. /**
  100. * Changes the muted state.
  101. *
  102. * @param {boolean} audioMuted - Whether audio should be muted or not.
  103. * @protected
  104. * @returns {void}
  105. */
  106. _setAudioMuted(audioMuted: boolean) {
  107. sendAnalytics(createToolbarEvent(AUDIO_MUTE, { enable: audioMuted }));
  108. this.props.dispatch(setAudioMuted(audioMuted));
  109. // FIXME: The old conference logic as well as the shared video feature
  110. // still rely on this event being emitted.
  111. typeof APP === 'undefined'
  112. || APP.UI.emitEvent(UIEvents.AUDIO_MUTED, audioMuted, true);
  113. }
  114. }
  115. /**
  116. * Maps (parts of) the redux state to the associated props for the
  117. * {@code AudioMuteButton} component.
  118. *
  119. * @param {Object} state - The Redux state.
  120. * @private
  121. * @returns {{
  122. * _audioMuted: boolean
  123. * }}
  124. */
  125. function _mapStateToProps(state): Object {
  126. const tracks = state['features/base/tracks'];
  127. return {
  128. _audioMuted: isLocalTrackMuted(tracks, MEDIA_TYPE.AUDIO)
  129. };
  130. }
  131. export default translate(connect(_mapStateToProps)(AudioMuteButton));