one-hook
Size
0.49 kb
View source

useMediaRecorder

Record media streams.

Installation

npm install @1hook/use-media-recorder

Usage

import {  } from '@1hook/use-media-recorder'

The recording automatically starts when a media stream is provided and stops when the stream is nullish.

(, {
  () {
    const  = new (, { : 'video/webm' })
    const  = .()
    ()
  },
})

You can pause and resume the recording:

const { ,  } = (, {
  () {
    // transform the chunks into a file or url
  },
})

Transform the recorded data into a File:

const { ,  } = (, {
  () {
    const  = 'video/webm'
    const  = new (, {  })
    const  = new ([], 'recording.webm', {  })
    ()
  },
})

Example

Record video from the device's camera:

import {  } from '@1hook/use-media-recorder'
import {  } from '@1hook/use-user-media'
import {  } from 'react'

export default function () {
  const [, ] = <string>()

  const  = ({ : true })

  const  = (., {
    () {
      const  = new (, { : 'video/webm' })
      const  = .()
      ()
    },
  })

  return (
    <>
      { && < ={}  />}

      {. === 'inactive' && (
        < ={.}>Start Recording</>
      )}
      {. === 'recording' && (
        < ={.}>Stop Recording</>
      )}
    </>
  )
}

API Reference

UseMediaRecorderOptions

The options of the useMediaRecorder hook. See MDN docs  for more details.

PropTypeDefault
onComplete?
((chunks: Blob[]) => void)
-
onResume?
(() => void)
-
onPause?
(() => void)
-
onStart?
(() => void)
-
onStop?
(() => void)
-
timeslice?
number
-
videoBitsPerSecond?
number
-
mimeType?
string
-
bitsPerSecond?
number
-
audioBitsPerSecond?
number
-

UseMediaRecorderReturn

The return value of the useMediaRecorder hook.

PropTypeDefault
resume
() => void
-
pause
() => void
-
state
RecordingState
-

On this page