React component for wavesurfer.js #3452
Replies: 3 comments 13 replies
-
O M G... huge! my only advice here would be to maybe also create a example:
|
Beta Was this translation helpful? Give feedback.
-
Thanks for your work @katspaugh. Do you have any hints on how to use a bit more 'advanced' features of your library like regions? Currently struggling to get this properly implemented with react, since I haven't found a way how to get the region instance. Edit: Actually wasn't that complicated, can get the plugin reference like this: const regionsPlugin = useMemo(() => RegionsPlugin.create(), []);
const plugins = useMemo(() => [regionsPlugin], [regionsPlugin]);
const containerRef = useRef<HTMLDivElement>(null);
const { wavesurfer, isPlaying } = useWavesurfer({
container: containerRef,
height: 64,
url: url,
peaks: peaks,
duration: duration,
plugins: plugins,
}); Then simply add a region like: regionsPlugin.addRegion({
content: `TestRegion`,
start: wavesurfer.getCurrentTime(),
end: wavesurfer.getCurrentTime() + 30,
}); Alternatively can add the return value of |
Beta Was this translation helpful? Give feedback.
-
@katspaugh I encountered something today where I'm not quite sure whether its a bug or me being dumb. When using interface TestProps {
url: string;
}
export const TestComponent = ({ url }: TestProps) => {
const containerRef = useRef<HTMLDivElement>(null);
const { wavesurfer, isPlaying } = useWavesurfer({
container: containerRef,
url: url,
interact: true,
normalize: true,
mediaControls: true,
});
const onZoomChange = useCallback(
(_: Event, newValue: number | number[]) => {
if (!wavesurfer) return;
wavesurfer.zoom(newValue as number);
},
[wavesurfer]
);
return (
<div
style={{
display: "flex",
flexDirection: "column",
width: "100%",
height: "100%",
}}
>
<div ref={containerRef} className="Container" />
<div style={{ width: "300px", padding: "16px" }}>
<Slider onChange={onZoomChange} min={0} max={150} />
</div>
</div>
);
}; The resulting HTML looks like: When I zoom in a bit: So However, lets say I have a somewhat responsive UI so I'm not setting a fixed width when initializing wavesurfer or the Setting it like div ref={containerRef} className="Container" style={{overflow: 'hidden'}} /> doesn't do anything, since there's an additional wrapper div being rendered as a child of my container. Setting Any hints on how to solve this? Doesn't seem like there's an easy way to target that container div, as it doesn't exist when my component first renders. |
Beta Was this translation helpful? Give feedback.
-
I've just published a new package –
@wavesurfer/react
– a React component for wavesurfer.js.It makes it easy to use wavesurfer from React. All of the familiar wavesurfer options become React props. You can subscribe to various wavesurfer events also via props. Just prepend an event name with
on
, e.g.ready
->onReady
.Please let me know if you have any feedback, suggestions for improvements are welcome!
I've also created a new NPM organization
@wavesurfer
where all future official packages will reside, and this React wrapper is the first one.Usage
Links & docs
GitHub: https://github.com/katspaugh/wavesurfer-react
NPM: https://www.npmjs.com/package/@wavesurfer/react
Beta Was this translation helpful? Give feedback.
All reactions