react-media-recorder

DeltaCircuitMIT长期未更新

基于 MediaRecorder API 的 React 录音录像组件,用 render prop 方式在网页里快速实现麦克风、摄像头和屏幕录制。

react-media-recorder is a react component with render prop that can be used to record audio/video streams using MediaRecorder API.

1.4.0发布于 2020-10-22 07:52 UTC586star133fork同步于 2026-11-07 07:33 UTC

怎么上手

由 README 提炼,执行前请确认
npm i react-media-recorder
硬件要求按 README 说明

README 未说明;依赖浏览器 MediaRecorder API,屏幕录制需浏览器支持 getDisplayMedia

下载:源码 zip·GitHub Releases(安装包)本站不镜像安装包

项目说明

由 README 自动翻译提炼

基于 MediaRecorder API 的 React 组件与 Hook,帮你快速实现音视频和屏幕录制

基于 MediaRecorder API 的 React 录音录像组件,用 render prop 方式在网页里快速实现麦克风、摄像头和屏幕录制。

核心能力

  • 提供 ReactMediaRecorder 组件与 useReactMediaRecorder Hook 两种用法
  • 支持录制音频、视频以及屏幕(getDisplayMedia)
  • 暴露 startRecording、pauseRecording、resumeRecording、stopRecording 等控制函数
  • 通过 render prop 返回 status、mediaBlobUrl、previewStream 等状态与数据
  • 支持 muteAudio/unmuteAudio 静音控制与 askPermissionOnMount 挂载时申请权限

容易踩的坑

屏幕录制并非所有浏览器都支持,需先确认 getDisplayMedia 兼容性;同时传 screen 和 video 时屏幕捕获优先,video 仅作为约束参数生效;mediaRecorderOptions 中的 MIME 类型会覆盖 audio/video 属性里指定的类型

内容由模型根据 README 生成,可能不完整或不准确,以官方文档为准。看原文 README

最近发版

4 个版本1.4.0
  1. 1.4.0useReactMediaRecorder hook最新正式版2020-10-22 · 2020-10-22 07:52 UTC

    useReactMediaRecorder

  2. 1.3.02020-06-30 · 2020-06-30 01:59 UTC

    这个版本没有写更新说明。

  3. 1.2.1V1.2.12020-06-29 · 2020-06-29 18:46 UTC

    这个版本没有写更新说明。

  4. 1.2.02020-05-08 · 2020-05-08 18:25 UTC
    查看更新说明

    Complete re-write with React Hooks and all.

用户点评

登录用户的真实使用记录,可按版本和硬件筛选
共 0 条

    还没有人点评。第一个说出真实体验的人最有价值。

    README 原文

    未翻译

    react-media-recorder :o2: :video_camera: :microphone: :computer:

    react-media-recorder is a fully typed react component with render prop, or a react hook, that can be used to:

    • Record audio/video
    • Record screen

    using MediaRecorder API.

    Installation

    npm i react-media-recorder
    

    or

    yarn add react-media-recorder
    

    Usage

    import { ReactMediaRecorder } from "react-media-recorder";
    
    const RecordView = () => (
      <div>
        <ReactMediaRecorder
          video
          render={({ status, startRecording, stopRecording, mediaBlobUrl }) => (
            <div>
              <p>{status}</p>
              <button onClick={startRecording}>Start Recording</button>
              <button onClick={stopRecording}>Stop Recording</button>
              <video src={mediaBlobUrl} controls autoPlay loop />
            </div>
          )}
        />
      </div>
    );
    

    Since react-media-recording uses render prop, you can define what to render in the view. Just don't forget to wire the startRecording, stopRecording and mediaBlobUrl to your component.

    Usage with react hooks

    import { useReactMediaRecorder } from "react-media-recorder";
    
    const RecordView = () => {
      const { status, startRecording, stopRecording, mediaBlobUrl } =
        useReactMediaRecorder({ video: true });
    
      return (
        <div>
          <p>{status}</p>
          <button onClick={startRecording}>Start Recording</button>
          <button onClick={stopRecording}>Stop Recording</button>
          <video src={mediaBlobUrl} controls autoPlay loop />
        </div>
      );
    };
    

    The hook receives an object as argument with the same ReactMediaRecorder options / props (except the render function).

    Options / Props

    audio

    Can be either a boolean value or a MediaTrackConstraints object.

    type: boolean or object default: true

    blobPropertyBag

    From MDN: An optional BlobPropertyBag dictionary which may specify the following two attributes (for the mediaBlob):

    • type, that represents the MIME type of the content of the array that will be put in the blob.
    • endings, with a default value of "transparent", that specifies how strings containing the line ending character \n are to be written out. It is one of the two values: "native", meaning that line ending characters are changed to match host OS filesystem convention, or "transparent", meaning that endings are stored in the blob without change

    type: object default: if video is enabled,

    {
       type: "video/mp4"
    }
    

    if there's only audio is enabled,

    {
      type: "audio/wav"
    }
    
    customMediaStream

    A media stream object itself (optional)

    mediaRecorderOptions

    An optional options object that will be passed to MediaRecorder. Please note that if you specify the MIME type via either audio or video prop and through this mediaRecorderOptions, the mediaRecorderOptions have higher precedence.

    type: object default: {}

    onStart

    A function that would get invoked when the MediaRecorder starts.

    type: function() default: () => null

    onStop

    A function that would get invoked when the MediaRecorder stops. It'll provide the blob and the blob url as its params.

    type: function(blobUrl: string, blob: Blob) default: () => null

    stopStreamsOnStop

    Whether to stop all streams on stop. By default, its true

    render

    A function which accepts an object containing fields: status, startRecording, stopRecording andmediaBlob. This function would return a react element/component.

    type: function default: () => null

    screen

    A boolean value. Lets you to record your current screen. Not all browsers would support this. Please check here for the availability. Please note that at the moment, the MediaRecorder won't record two alike streams at a time, if you provide both screen and video prop, the screen capturing will take precedence than the video capturing. But, you can provide the video prop (as the MediaTrackConstraints) which will then utilized by screen capture (for example, height, width etc..)

    video

    Can be either a boolean value or a MediaTrackConstraints object.

    type: boolean or object default: false

    askPermissionOnMount

    A boolean value. If set to true, will ask media permission on mounting.

    type: boolean default: false

    preferCurrentTab

    A boolean value. If set to true, the browser will offer the current tab as the most prominent capture source, i.e. as a separate "This Tab" option in the "Choose what to share" options presented to the user.

    type: boolean default: false

    selfBrowserSurface

    An enumerated value specifying whether the browser should allow the user to select the current tab for capture. Possible values are include, which hints that the browser should include the current tab in the choices offered for capture, and exclude, which hints that it should be excluded.

    type: undefined | 'include' | 'exclude'; default: undefined

    Props available in the render function

    error

    A string enum. Possible values:

    • media_aborted
    • permission_denied
    • no_specified_media_found
    • media_in_use
    • invalid_media_constraints
    • no_constraints
    • recorder_error
    status

    A string enum. Possible values:

    • media_aborted
    • permission_denied
    • no_specified_media_found
    • media_in_use
    • invalid_media_constraints
    • no_constraints
    • recorder_error
    • idle
    • acquiring_media
    • recording
    • stopping
    • stopped
    startRecording

    A function, which starts recording when invoked.

    pauseRecording

    A function, which pauses the recording when invoked.

    resumeRecording

    A function, which resumes the recording when invoked.

    stopRecording

    A function, which stops recording when invoked.

    muteAudio

    A function, which mutes the audio tracks when invoked.

    unmuteAudio

    A function which unmutes the audio tracks when invoked.

    mediaBlobUrl

    A blob url that can be wired to an <audio />, <video /> or an <a /> element.

    clearBlobUrl

    A function which clears the existing generated blob url (if any) and resets the workflow to its initial idle state.

    isMuted

    A boolean prop that tells whether the audio is muted or not.

    previewStream

    If you want to create a live-preview of the video to the user, you can use this stream and attach it to a <video /> element. Please note that this is a muted stream. This is by design to get rid of internal microphone feedbacks on machines like laptop.

    For example:

    const VideoPreview = ({ stream }: { stream: MediaStream | null }) => {
      const videoRef = useRef<HTMLVideoElement>(null);
    
      useEffect(() => {
        if (videoRef.current && stream) {
          videoRef.current.srcObject = stream;
        }
      }, [stream]);
      if (!stream) {
        return null;
      }
      return <video ref={videoRef} width={500} height={500} autoPlay controls />;
    };
    
    const App = () => (
      <ReactMediaRecorder
        video
        render={({ previewStream }) => {
          return <VideoPreview stream={previewStream} />;
        }}
      />
    );
    
    previewAudioStream

    If you want access to the live audio stream for use in sound visualisations, you can use this stream as your audio source and extract data from it using the AudioContext and AnalyzerNode features of the Web Audio API. Some javascript examples of how to do this can be found here.

    Contributing

    Feel free to submit a PR if you found a bug (I might've missed many! :grinning:) or if you want to enhance it further.

    Thanks!. Happy Recording!