Skip to content

Commit 2ea53cb

Browse files
committed
improve react example
1 parent c165132 commit 2ea53cb

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

README.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,12 @@ const playerPlayingProgressCallback = (progressInPercent) => {
153153

154154
To see an example of what it looks like when the range gets modified, check out the animated gif on top of this README or install the example from this repository, which uses an audio player that redraws the waveform element every time its progress callback gets triggered: [simple waveform and audio player example](/examples/simple-waveform/README.md)
155155

156+
If you want to replace the current waveform display with another waveform because your audio player got told to play the next song, then you don't need to create another waveform, just replace the wave peaks from the previous song with the peaks of the current song,like so:
157+
158+
```ts
159+
waveform.setWaveData(peaksArray)
160+
```
161+
156162
### React component example
157163

158164
Note: This code does the exact same thing as the previous example but uses React instead of vanilla javascript
@@ -166,15 +172,25 @@ import { Waveform, IWaveLayoutOptions, IWaveCoreOptions } from 'waveform-visuali
166172
const WaveformComponent = () => {
167173

168174
const canvasRef = useRef<HTMLCanvasElement>()
175+
const waveformRef = useRef<Waveform | null>(null)
169176

170177
const onWaveClickHandler = useCallback((clickHorizontalPositionInPercent: number): void => {
171178

172179
console.log('waveformClickCallback: ', clickHorizontalPositionInPercent)
173180

174-
// tell the audio player to move to 50% of the sound
181+
// tell your audio player to move to 50% of the sound
182+
// if you need an audio player checkout my other project:
183+
// https://github.com/chrisweb/web-audio-api-player
184+
//playerRef.current.goToPosition(clickHorizontalPositionInPercent)
175185

176186
}, [])
177187

188+
// here is some pseudo code to show you what to when another sound (song) gets played
189+
//playerRef.current.onPlayCallback((sound) => {
190+
// const peaksArray = sound.waveData
191+
// waveformRef.current.setWaveData(peaksArray)
192+
//})
193+
178194
const initializeWaveform = useCallback(() => {
179195

180196
const waveLayoutOptions: IWaveLayoutOptions = {
@@ -191,6 +207,8 @@ const WaveformComponent = () => {
191207

192208
const waveform = new Waveform(waveCoreOptions)
193209

210+
waveformRef.current = waveform
211+
194212
waveform.setCanvasElement(canvasRef.current)
195213

196214
waveform.draw(0)
@@ -199,6 +217,13 @@ const WaveformComponent = () => {
199217

200218
useEffect(() => {
201219
initializeWaveform()
220+
return () => {
221+
if (waveformRef.current !== null) {
222+
// on component umount, destroy the waveform
223+
// the waveform will remove the click event listener
224+
waveformRef.current.destroy()
225+
}
226+
}
202227
}, [initializeWaveform])
203228

204229
return (

0 commit comments

Comments
 (0)