@wavesurfer/react
TypeScript icon, indicating that this package has built-in type declarations

1.0.7 • Public • Published

@wavesurfer/react

npm

A React component and hook 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. Each event callback receives a wavesurfer instance as the first argument.

Installation

With yarn:

yarn add wavesurfer.js @wavesurfer/react

With npm:

npm install wavesurfer.js @wavesurfer/react

Usage

As a component:

import WavesurferPlayer from '@wavesurfer/react'

const App = () => {
  const [wavesurfer, setWavesurfer] = useState(null)
  const [isPlaying, setIsPlaying] = useState(false)

  const onReady = (ws) => {
    setWavesurfer(ws)
    setIsPlaying(false)
  }

  const onPlayPause = () => {
    wavesurfer && wavesurfer.playPause()
  }

  return (
    <>
      <WavesurferPlayer
        height={100}
        waveColor="violet"
        url="/my-server/audio.wav"
        onReady={onReady}
        onPlay={() => setIsPlaying(true)}
        onPause={() => setIsPlaying(false)}
      />

      <button onClick={onPlayPause}>
        {isPlaying ? 'Pause' : 'Play'}
      </button>
    </>
  )
}

Alternatively, as a hook:

import { useRef } from 'react'
import { useWavesurfer } from '@wavesurfer/react'

const App = () => {
  const containerRef = useRef(null)

  const { wavesurfer, isReady, isPlaying, currentTime } = useWavesurfer({
    container: containerRef,
    url: '/my-server/audio.ogg',
    waveColor: 'purple',
    height: 100,
  })

  const onPlayPause = () => {
    wavesurfer && wavesurfer.playPause()
  }

  return (
    <>
      <div ref={containerRef} />

      <button onClick={onPlayPause}>
        {isPlaying ? 'Pause' : 'Play'}
      </button>
    </>
  )
}

Docs

https://wavesurfer.xyz

Dependents (5)

Package Sidebar

Install

npm i @wavesurfer/react

Weekly Downloads

20,489

Version

1.0.7

License

BSD-3-Clause

Unpacked Size

10.5 kB

Total Files

5

Last publish

Collaborators

  • katspaugh