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

AudioInputPreview.web.tsx 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import React, { useEffect, useState } from 'react';
  2. import { makeStyles } from 'tss-react/mui';
  3. import JitsiMeetJS from '../../base/lib-jitsi-meet/_.web';
  4. const JitsiTrackEvents = JitsiMeetJS.events.track;
  5. /**
  6. * The type of the React {@code Component} props of {@link AudioInputPreview}.
  7. */
  8. interface IProps {
  9. /**
  10. * The JitsiLocalTrack to show an audio level meter for.
  11. */
  12. track: any;
  13. }
  14. const useStyles = makeStyles()(theme => {
  15. return {
  16. container: {
  17. display: 'flex'
  18. },
  19. section: {
  20. flex: 1,
  21. height: '4px',
  22. borderRadius: '1px',
  23. backgroundColor: theme.palette.ui04,
  24. marginRight: theme.spacing(1),
  25. '&:last-of-type': {
  26. marginRight: 0
  27. }
  28. },
  29. activeSection: {
  30. backgroundColor: theme.palette.success01
  31. }
  32. };
  33. });
  34. const NO_OF_PREVIEW_SECTIONS = 11;
  35. const AudioInputPreview = (props: IProps) => {
  36. const [ audioLevel, setAudioLevel ] = useState(0);
  37. const { classes, cx } = useStyles();
  38. /**
  39. * Starts listening for audio level updates from the library.
  40. *
  41. * @param {JitsiLocalTrack} track - The track to listen to for audio level
  42. * updates.
  43. * @private
  44. * @returns {void}
  45. */
  46. function _listenForAudioUpdates(track: any) {
  47. _stopListeningForAudioUpdates();
  48. track?.on(
  49. JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED,
  50. setAudioLevel);
  51. }
  52. /**
  53. * Stops listening to further updates from the current track.
  54. *
  55. * @private
  56. * @returns {void}
  57. */
  58. function _stopListeningForAudioUpdates() {
  59. props.track?.off(
  60. JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED,
  61. setAudioLevel);
  62. }
  63. useEffect(() => {
  64. _listenForAudioUpdates(props.track);
  65. return _stopListeningForAudioUpdates;
  66. }, []);
  67. useEffect(() => {
  68. _listenForAudioUpdates(props.track);
  69. setAudioLevel(0);
  70. }, [ props.track ]);
  71. const audioMeterFill = Math.ceil(Math.floor(audioLevel * 100) / (100 / NO_OF_PREVIEW_SECTIONS));
  72. return (
  73. <div className = { classes.container } >
  74. {new Array(NO_OF_PREVIEW_SECTIONS).fill(0)
  75. .map((_, idx) =>
  76. (<div
  77. className = { cx(classes.section, idx < audioMeterFill && classes.activeSection) }
  78. key = { idx } />)
  79. )}
  80. </div>
  81. );
  82. };
  83. export default AudioInputPreview;