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

AudioInputPreview.js 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import PropTypes from 'prop-types';
  2. import React, { PureComponent } from 'react';
  3. import { JitsiTrackEvents } from '../../base/lib-jitsi-meet';
  4. /**
  5. * React component for displaying a audio level meter for a JitsiLocalTrack.
  6. */
  7. class AudioInputPreview extends PureComponent {
  8. /**
  9. * AudioInputPreview component's property types.
  10. *
  11. * @static
  12. */
  13. static propTypes = {
  14. /*
  15. * The JitsiLocalTrack to show an audio level meter for.
  16. */
  17. track: PropTypes.object
  18. };
  19. /**
  20. * Initializes a new AudioInputPreview instance.
  21. *
  22. * @param {Object} props - The read-only React Component props with which
  23. * the new instance is to be initialized.
  24. */
  25. constructor(props) {
  26. super(props);
  27. this.state = {
  28. audioLevel: 0
  29. };
  30. this._updateAudioLevel = this._updateAudioLevel.bind(this);
  31. }
  32. /**
  33. * Starts listening for audio level updates after the initial render.
  34. *
  35. * @inheritdoc
  36. * @returns {void}
  37. */
  38. componentDidMount() {
  39. this._listenForAudioUpdates(this.props.track);
  40. }
  41. /**
  42. * Stops listening for audio level updates on the old track and starts
  43. * listening instead on the new track.
  44. *
  45. * @inheritdoc
  46. * @returns {void}
  47. */
  48. componentWillReceiveProps(nextProps) {
  49. this._listenForAudioUpdates(nextProps.track);
  50. this._updateAudioLevel(0);
  51. }
  52. /**
  53. * Unsubscribe from audio level updates.
  54. *
  55. * @inheritdoc
  56. * @returns {void}
  57. */
  58. componentWillUnmount() {
  59. this._stopListeningForAudioUpdates();
  60. }
  61. /**
  62. * Implements React's {@link Component#render()}.
  63. *
  64. * @inheritdoc
  65. * @returns {ReactElement}
  66. */
  67. render() {
  68. const audioMeterFill = {
  69. width: `${Math.floor(this.state.audioLevel * 100)}%`
  70. };
  71. return (
  72. <div className = 'audio-input-preview' >
  73. <div
  74. className = 'audio-input-preview-level'
  75. style = { audioMeterFill } />
  76. </div>
  77. );
  78. }
  79. /**
  80. * Starts listening for audio level updates from the library.
  81. *
  82. * @param {JitstiLocalTrack} track - The track to listen to for audio level
  83. * updates.
  84. * @private
  85. * @returns {void}
  86. */
  87. _listenForAudioUpdates(track) {
  88. this._stopListeningForAudioUpdates();
  89. track && track.on(
  90. JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED,
  91. this._updateAudioLevel);
  92. }
  93. /**
  94. * Stops listening to further updates from the current track.
  95. *
  96. * @private
  97. * @returns {void}
  98. */
  99. _stopListeningForAudioUpdates() {
  100. this.props.track && this.props.track.off(
  101. JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED,
  102. this._updateAudioLevel);
  103. }
  104. /**
  105. * Updates the internal state of the last know audio level. The level should
  106. * be between 0 and 1, as the level will be used as a percentage out of 1.
  107. *
  108. * @param {number} audioLevel - The new audio level for the track.
  109. * @private
  110. * @returns {void}
  111. */
  112. _updateAudioLevel(audioLevel) {
  113. this.setState({
  114. audioLevel
  115. });
  116. }
  117. }
  118. export default AudioInputPreview;