You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AudioInputPreview.js 3.2KB

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