Skip to content

Instantly share code, notes, and snippets.

@VadimBrodsky
Created August 7, 2020 02:40
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VadimBrodsky/f4ad0b48a79fc02307d3271e9d60460f to your computer and use it in GitHub Desktop.
Save VadimBrodsky/f4ad0b48a79fc02307d3271e9d60460f to your computer and use it in GitHub Desktop.
RecordRTC React Example
import React, { useState, useRef, useEffect } from 'react';
import './App.css';
import RecordRTC, { invokeSaveAsDialog } from 'recordrtc';
function App() {
const [stream, setStream] = useState(null);
const [blob, setBlob] = useState(null);
const refVideo = useRef(null);
const recorderRef = useRef(null);
const handleRecording = async () => {
// const cameraStream = await navigator.mediaDevices.getUserMedia({ video: true, audio: true });
const mediaStream = await navigator.mediaDevices.getDisplayMedia({
video: {
width: 1920,
height: 1080,
frameRate: 30,
},
audio: false,
});
setStream(mediaStream);
recorderRef.current = new RecordRTC(mediaStream, { type: 'video' });
recorderRef.current.startRecording();
};
const handleStop = () => {
recorderRef.current.stopRecording(() => {
setBlob(recorderRef.current.getBlob());
});
};
const handleSave = () => {
invokeSaveAsDialog(blob);
};
useEffect(() => {
if (!refVideo.current) {
return;
}
// refVideo.current.srcObject = stream;
}, [stream, refVideo]);
return (
<div className="App">
<header className="App-header">
<button onClick={handleRecording}>start</button>
<button onClick={handleStop}>stop</button>
<button onClick={handleSave}>save</button>
{blob && (
<video
src={URL.createObjectURL(blob)}
controls
autoPlay
ref={refVideo}
style={{ width: '700px', margin: '1em' }}
/>
)}
</header>
</div>
);
}
export default App;
@rainbow-jj
Copy link

you are very great,thankyou~!!!

@adshin21
Copy link

adshin21 commented Nov 6, 2021

I'm not able to get camera preview while recording, is there any way to get like this in react ?

@TausifM
Copy link

TausifM commented Apr 28, 2022

Hi How to convert it into file and send it to backend please add that file in video format

@santo4ul
Copy link

Hi, Thanks for the script.

Recording from local microphone is clear to me.

Question: How do I record the remote audio (incoming audio) using RecordRTC?

Thank you.

@fukemy
Copy link

fukemy commented Jan 19, 2024

hi, any React Native solution?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment