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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import PropTypes from 'prop-types';
  2. import React, { Component } 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 Component {
  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. if (nextProps.track !== this.props.track) {
  50. this._listenForAudioUpdates(nextProps.track);
  51. this._updateAudioLevel(0);
  52. }
  53. }
  54. /**
  55. * Unsubscribe from audio level updates.
  56. *
  57. * @inheritdoc
  58. * @returns {void}
  59. */
  60. componentWillUnmount() {
  61. this._stopListeningForAudioUpdates();
  62. }
  63. /**
  64. * Implements React's {@link Component#render()}.
  65. *
  66. * @inheritdoc
  67. * @returns {ReactElement}
  68. */
  69. render() {
  70. const audioMeterFill = {
  71. width: `${Math.floor(this.state.audioLevel * 100)}%`
  72. };
  73. return (
  74. <div className = 'audio-input-preview' >
  75. <div
  76. className = 'audio-input-preview-level'
  77. style = { audioMeterFill } />
  78. </div>
  79. );
  80. }
  81. /**
  82. * Starts listening for audio level updates from the library.
  83. *
  84. * @param {JitstiLocalTrack} track - The track to listen to for audio level
  85. * updates.
  86. * @private
  87. * @returns {void}
  88. */
  89. _listenForAudioUpdates(track) {
  90. this._stopListeningForAudioUpdates();
  91. track && track.on(
  92. JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED,
  93. this._updateAudioLevel);
  94. }
  95. /**
  96. * Stops listening to further updates from the current track.
  97. *
  98. * @private
  99. * @returns {void}
  100. */
  101. _stopListeningForAudioUpdates() {
  102. this.props.track && this.props.track.off(
  103. JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED,
  104. this._updateAudioLevel);
  105. }
  106. /**
  107. * Updates the internal state of the last know audio level. The level should
  108. * be between 0 and 1, as the level will be used as a percentage out of 1.
  109. *
  110. * @param {number} audioLevel - The new audio level for the track.
  111. * @private
  112. * @returns {void}
  113. */
  114. _updateAudioLevel(audioLevel) {
  115. this.setState({
  116. audioLevel
  117. });
  118. }
  119. }
  120. export default AudioInputPreview;