-
Hi, import { useCallback, useEffect, useRef, useState } from 'react'
import WaveSurfer from 'wavesurfer.js'
import RegionsPlugin from 'wavesurfer.js/dist/plugins/regions'
import { Button } from './components'
const MAX_FREQUENCY = 8000
const SHOW_PLAYER = true
const Waveform = ({
audioElementRef,
src,
}: {
src: string
audioElementRef: React.RefObject<HTMLAudioElement>
}) => {
const waveformRef = useRef<HTMLDivElement>(null)
const wavesurferRef = useRef<WaveSurfer | null>(null)
const regionsPluginRef = useRef<RegionsPlugin | null>(null)
const [regionColor, setRegionColor] = useState('red')
useEffect(() => {
if (audioElementRef.current && waveformRef.current) {
const regions = RegionsPlugin.create()
regionsPluginRef.current = regions
wavesurferRef.current = WaveSurfer.create({
container: waveformRef.current,
height: SHOW_PLAYER ? 150 : 0,
sampleRate: MAX_FREQUENCY * 2,
plugins: [regions],
media: audioElementRef.current,
// url: src,
barAlign: 'bottom',
barHeight: 30,
barWidth: 2,
waveColor: 'rgb(2, 132, 199)',
progressColor: 'grey',
})
return () => {
wavesurferRef.current?.destroy()
}
}
return () => {}
}, [audioElementRef, src])
useEffect(() => {
if (regionsPluginRef.current) {
regionsPluginRef.current.enableDragSelection({
color: regionColor,
})
}
}, [regionColor])
const changeRegionColor = useCallback(() => {
const newColor = regionColor === 'red' ? 'blue' : 'red'
setRegionColor(newColor)
}, [regionColor])
function togglePlay() {
wavesurferRef.current?.playPause()
}
const isPlaying = wavesurferRef.current?.isPlaying()
return (
<div className="flex flex-row items-center gap-4">
<Button
className="flex h-20 w-20 flex-none items-center justify-center rounded-full text-2xl"
onMouseUp={togglePlay}
>
{isPlaying ? '❚❚' : '▶'}
</Button>
<Button
className="flex h-20 w-20 flex-none items-center justify-center rounded-full text-2xl"
onMouseUp={changeRegionColor}
>
{regionColor}
</Button>
<div className="relative flex w-full flex-col">
<div ref={waveformRef} />
</div>
</div>
)
}
export default Waveform With my current code, it is working, but there is a small bug behind :
am I missing something ? is there a better way to do this ? |
Beta Was this translation helpful? Give feedback.
Answered by
katspaugh
Jul 31, 2024
Replies: 1 comment 1 reply
-
The only possible problem I could spot with your code is this:
This will enable drag selection multiple times which is not something wavesurfer expects. It can lead to the drag gesture firing multiple times, i.e. creating multiple regions at once. You can fix it by adding a return:
|
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
anbotz
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The only possible problem I could spot with your code is this:
This will enable drag selection multiple times which is not something wavesurfer expects. It can lead to the drag gesture firing multiple times, i.e. creating multiple regions at once. You can fix it by adding a return: