選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

VolumeSlider.tsx 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* eslint-disable lines-around-comment*/
  2. import Slider from '@react-native-community/slider';
  3. import { throttle } from 'lodash-es';
  4. import React, { PureComponent } from 'react';
  5. import { View, ViewStyle } from 'react-native';
  6. import { connect } from 'react-redux';
  7. import { IReduxState } from '../../../app/types';
  8. import Icon from '../../../base/icons/components/Icon';
  9. import { IconVolumeUp } from '../../../base/icons/svg';
  10. import { MEDIA_TYPE } from '../../../base/media/constants';
  11. import {
  12. getTrackByMediaTypeAndParticipant,
  13. getTrackState
  14. } from '../../../base/tracks/functions.native';
  15. import BaseTheme from '../../../base/ui/components/BaseTheme.native';
  16. import { setVolume } from '../../../participants-pane/actions.native';
  17. import { NATIVE_VOLUME_SLIDER_SCALE } from '../../constants';
  18. import styles from './styles';
  19. /**
  20. * The type of the React {@code Component} props of {@link VolumeSlider}.
  21. */
  22. interface IProps {
  23. /**
  24. * Whether the participant enters the conference silent.
  25. */
  26. _startSilent?: boolean;
  27. /**
  28. * Remote audio track.
  29. */
  30. _track?: any;
  31. /**
  32. * The volume level for the participant.
  33. */
  34. _volume?: number;
  35. /**
  36. * The redux dispatch function.
  37. */
  38. dispatch?: Function;
  39. /**
  40. * The ID of the participant.
  41. */
  42. participantID?: string;
  43. }
  44. /**
  45. * The type of the React {@code Component} state of {@link VolumeSlider}.
  46. */
  47. interface IState {
  48. /**
  49. * The volume of the participant's audio element. The value will
  50. * be represented by a slider.
  51. */
  52. volumeLevel: number;
  53. }
  54. /**
  55. * Component that renders the volume slider.
  56. *
  57. * @returns {React$Element<any>}
  58. */
  59. class VolumeSlider extends PureComponent<IProps, IState> {
  60. _originalVolumeChange: Function;
  61. /**
  62. * Initializes a new {@code VolumeSlider} instance.
  63. *
  64. * @param {Object} props - The read-only properties with which the new
  65. * instance is to be initialized.
  66. */
  67. constructor(props: IProps) {
  68. super(props);
  69. this.state = {
  70. volumeLevel: props._volume || Math.ceil(NATIVE_VOLUME_SLIDER_SCALE / 2)
  71. };
  72. this._originalVolumeChange = this._onVolumeChange;
  73. this._onVolumeChange = throttle(
  74. volumeLevel => this._originalVolumeChange(volumeLevel), 500
  75. );
  76. }
  77. /**
  78. * Implements React's {@link Component#render()}.
  79. *
  80. * @inheritdoc
  81. * @returns {ReactElement}
  82. */
  83. render() {
  84. const { _startSilent } = this.props;
  85. const { volumeLevel } = this.state;
  86. const onVolumeChange = _startSilent ? undefined : this._onVolumeChange;
  87. return (
  88. <View style = { styles.volumeSliderContainer as ViewStyle } >
  89. <Icon
  90. size = { 24 }
  91. src = { IconVolumeUp } />
  92. <Slider
  93. maximumTrackTintColor = { BaseTheme.palette.ui10 }
  94. maximumValue = { NATIVE_VOLUME_SLIDER_SCALE }
  95. minimumTrackTintColor = { BaseTheme.palette.action01 }
  96. minimumValue = { 0 }
  97. onValueChange = { onVolumeChange }
  98. style = { styles.sliderContainer as ViewStyle }
  99. thumbTintColor = { BaseTheme.palette.ui10 }
  100. value = { volumeLevel } />
  101. </View>
  102. );
  103. }
  104. /**
  105. * Sets the internal state of the volume level for the volume slider.
  106. * Invokes the prop onVolumeChange to notify of volume changes.
  107. *
  108. * @param {number} volumeLevel - Selected volume on slider.
  109. * @private
  110. * @returns {void}
  111. */
  112. _onVolumeChange(volumeLevel: any) {
  113. const { _track, dispatch, participantID } = this.props;
  114. const audioTrack = _track?.jitsiTrack.track;
  115. let newVolumeLevel;
  116. if (volumeLevel <= 10) {
  117. newVolumeLevel = volumeLevel / 10;
  118. } else {
  119. newVolumeLevel = volumeLevel - 9;
  120. }
  121. audioTrack?._setVolume(newVolumeLevel);
  122. // @ts-ignore
  123. dispatch(setVolume(participantID, newVolumeLevel));
  124. }
  125. }
  126. /**
  127. * Maps (parts of) the Redux state to the associated props for the
  128. * {@code VolumeSlider} component.
  129. *
  130. * @param {Object} state - The Redux state.
  131. * @param {Object} ownProps - The own props of the component.
  132. * @returns {IProps}
  133. */
  134. function mapStateToProps(state: IReduxState, ownProps: IProps) {
  135. const { participantID } = ownProps;
  136. const { participantsVolume } = state['features/filmstrip'];
  137. const { startSilent } = state['features/base/config'];
  138. const tracks = getTrackState(state);
  139. return {
  140. _startSilent: Boolean(startSilent),
  141. _track: getTrackByMediaTypeAndParticipant(tracks, MEDIA_TYPE.AUDIO, participantID),
  142. _volume: participantID && participantsVolume[participantID]
  143. };
  144. }
  145. // @ts-ignore
  146. export default connect(mapStateToProps)(VolumeSlider);