Skip to content

Not working with audio file in reactΒ #6

@shivam1317

Description

@shivam1317

Hey @jerosoler ,
I have tried the same code as per the documentation but its not working
Here is my component:

import React, { useEffect, useRef, useState } from "react";
import { getAudioData, linearPath, polarPath } from "waveform-path";

const VoiceAnalyzer = () => {
  const [pathLogo, setPathLogo] = useState(null);
  const audioRef = useRef(null);

  const audioFile = "Audio.mp3"; // Replace with your audio file path
  useEffect(() => {
    AudioPath("Audio.mp3");
    // AudioPath("https://jerosoler.github.io/waveform-path/hello_world.ogg");
  }, []);
  async function AudioPath(file) {
    const audioData = await getAudioData(file);

    const pathLogo = polarPath(audioData, {
      samples: 90,
      type: "steps",
      left: 200,
      top: 200,
      distance: 100,
      length: 100,
      normalize: true,
      // animation: true,
      paths: [
        { d: "L", sdeg: 0, sr: 0, edeg: 0, er: 90 },
        {
          d: "A",
          sdeg: 0,
          sr: 90,
          edeg: 100,
          er: 90,
          rx: 5,
          ry: 5,
          angle: 100,
          arc: 1,
          sweep: 1,
        },
        { d: "L", sdeg: 100, sr: 90, edeg: 100, er: 0 },
      ],
    });
    setPathLogo(pathLogo);
  }
  return (
    <div className="w-auto">
      {/* <audio ref={audioRef} src={audioFile} /> */}
      <svg id="logo" className="w-full h-screen">
        <path d={pathLogo}></path>
      </svg>
      {/* <button onClick={handlePlayPause}>{isPlaying ? "Pause" : "Play"}</button> */}
    </div>
  );
};

export default VoiceAnalyzer;

I also got an error saying:

The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page.

because its not allowed to play an audio automatically on chrome or other browsers i guess so i even added a button to play that file but still its not changing dynamically.

At last, i have tried to add animation:true and <animation> element in <path> is working as expected but its not based on the frequency of audioData, its just a random infinite animation.

Example:

<svg id="logo" className="w-full h-screen">
   <path>
          <animate
            attributeName="d"
            dur="5s"
            repeatCount="indefinite"
            calcMode="linear"
            values={pathLogo}
     />
  </path>
</svg>

Is there anything i am missing while changing the svg path dynamically based on the frequency of given audio file data?
Please let me know
Thanks

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions