Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Audio Recording Visualization Not Working #30

Open
iloveitaly opened this issue Dec 19, 2024 · 7 comments · May be fixed by #31
Open

Audio Recording Visualization Not Working #30

iloveitaly opened this issue Dec 19, 2024 · 7 comments · May be fixed by #31
Assignees

Comments

@iloveitaly
Copy link

Describe the bug
The audio recording visualization is not working:

CleanShot.2024-12-19.at.07.06.49.mp4

To Reproduce
Source code here:
https://stackblitz.com/edit/react-voice-visualizer?file=src%2Fcomponents%2Fvoice-recorder.tsx

Expected behavior
Audio visualization when recording

Screenshots
If applicable, add screenshots to help explain your problem.

Package info (please complete the following information):

  • Version: 2.0.4

Additional context
Add any other context about the problem here.

@nikhil2802Vishwakarma
Copy link

Hi, I got the same error. did you got any solution?

@Twathik
Copy link

Twathik commented Jan 20, 2025

I found the solution by luck

set backgroundColor="black"
to see the visualizer you have to tweek the classnames to adapt to your app

@nikhil2802Vishwakarma
Copy link

@Twathik Thank you for your help, you saved me from looking for a different plugin. But none of it is as good as this.

@Twathik
Copy link

Twathik commented Jan 20, 2025

you are welcome

@chinaza
Copy link

chinaza commented Feb 4, 2025

I have fought with this issue all day today and finally found an external fix and a suggestion for improving the library, @YZarytskyi. Please let me know how I can contribute, as this library is amazing, and this small enhancement would make it even better.

Issue and suggested fix in the library

The canvas dimensions are being set before the component is fully rendered, leading to potential zero-width scenarios. This occurs in VoiceVisualizer.tsx#L168:

useEffect(() => {
  onResize();  // Executes before proper layout

...

}, [width, isAvailableRecordedAudio]);

Proposed Solution

Replace the current resize handling with ResizeObserver for more reliable dimension management:

// Use useLayoutEffect for initial dimensions
useLayoutEffect(() => {
  if (!canvasContainerRef.current) return;
  
  const resizeObserver = new ResizeObserver(() => {
    onResize();
  });
  
  resizeObserver.observe(canvasContainerRef.current);
  
  return () => resizeObserver.disconnect();
}, []);

Benefits:

  • Reliable initial dimensions
  • Automatic response to size changes
  • Clean teardown
  • Better performance than window resize events

Current Workaround (what I am using right now)

Until this is implemented, users can wrap the component with this solution:

import { PropsWithChildren, useLayoutEffect, useRef, useState } from 'react';

export default function ComponentReadyWrapper({ children }: PropsWithChildren) {
  const [dimensions, setDimensions] = useState({ width: 0, height: 0 });
  const wrapperRef = useRef<HTMLDivElement>(null);

  useLayoutEffect(() => {
    if (!wrapperRef.current) return;

    const resizeObserver = new ResizeObserver(() => {
      if (wrapperRef.current) {
        setDimensions({
          width: wrapperRef.current.clientWidth,
          height: wrapperRef.current.clientHeight
        });
      }
    });

    resizeObserver.observe(wrapperRef.current);
    return () => resizeObserver.disconnect();
  }, []);

  return (
    <div ref={wrapperRef} style={{ width: '100%', height: '100%' }}>
      {dimensions.width > 0 && children}
    </div>
  );
}

Usage example:

<ComponentReadyWrapper>
            <VoiceVisualizer
              controls={recorderControls}
              height="194px"
              isDefaultUIShown={false}
              isControlPanelShown={isVnMode}
              mainBarColor="#669eff"
              secondaryBarColor="#ffd534"
            />
</ComponentReadyWrapper>

Would you be interested in a pull request implementing this change?

@YZarytskyi
Copy link
Owner

Hello, yes sure, you can contribute, I appreciate it

chinaza added a commit to chinaza/react-voice-visualizer that referenced this issue Feb 4, 2025
- Remove window resize event listener
 - Implement ResizeObserver for more accurate dimension tracking
 - Move resize logic to useLayoutEffect for synchronous measurements
 - Add proper cleanup with observer disconnect
 - Fix initial render dimensions calculation

BREAKING CHANGE: Removes window.resize event listener dependency

Closes YZarytskyi#30
@chinaza
Copy link

chinaza commented Feb 4, 2025

Done: #31

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants