Skip to content

Commit 7857905

Browse files
committed
Fix screen recorder and introduce previewStream
1 parent 7cb0dea commit 7857905

File tree

2 files changed

+49
-9
lines changed

2 files changed

+49
-9
lines changed

README.md

+38-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
# react-media-recorder :o2: :video_camera: :microphone: :computer:
22

3-
`react-media-recorder` is a fully typed react component with render prop that can be used to record audio/video streams or the screens using [MediaRecorder](https://developer.mozilla.org/en-US/docs/Web/API/MediaRecorder) API.
3+
`react-media-recorder` is a fully typed react component with render prop that can be used to:
4+
5+
- Record audio/video
6+
- Record screen
7+
8+
using [MediaRecorder API](https://developer.mozilla.org/en-US/docs/Web/API/MediaRecorder).
49

510
## Installation
611

712
```
8-
npm i -S react-media-recorder
13+
npm i react-media-recorder
914
```
1015

1116
or
@@ -168,6 +173,37 @@ A `blob` url that can be wired to an `<audio />`, `<video />` or an `<a />` elem
168173

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

176+
#### previewStream
177+
178+
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.
179+
180+
For example:
181+
182+
```tsx
183+
const VideoPreview = ({ stream }: { stream: MediaStream | null }) => {
184+
const videoRef = useRef<HTMLVideoElement>(null);
185+
186+
useEffect(() => {
187+
if (videoRef.current && stream) {
188+
videoRef.current.srcObject = stream;
189+
}
190+
}, [stream]);
191+
if (!stream) {
192+
return null;
193+
}
194+
return <video ref={videoRef} width={500} height={500} autoPlay controls />;
195+
};
196+
197+
const App = () => (
198+
<ReactMediaRecorder
199+
video
200+
render={({ videoPreviewStream }) => {
201+
return <VideoPreview stream={videoPreviewStream} />;
202+
}}
203+
/>
204+
);
205+
```
206+
171207
## Contributing
172208

173209
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.

src/index.ts

+11-7
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ type ReactMediaRecorderRenderProps = {
1111
mediaBlobUrl: null | string;
1212
status: StatusMessages;
1313
isAudioMuted: boolean;
14+
previewStream: MediaStream | null;
1415
};
1516

1617
type ReactMediaRecorderProps = {
@@ -76,7 +77,7 @@ export const ReactMediaRecorder = ({
7677
if (screen) {
7778
//@ts-ignore
7879
const stream = (await window.navigator.mediaDevices.getDisplayMedia({
79-
video
80+
video: video || true
8081
})) as MediaStream;
8182
if (audio) {
8283
const audioStream = await window.navigator.mediaDevices.getUserMedia({
@@ -88,12 +89,12 @@ export const ReactMediaRecorder = ({
8889
.forEach(audioTrack => stream.addTrack(audioTrack));
8990
}
9091
mediaStream.current = stream;
92+
} else {
93+
const stream = await window.navigator.mediaDevices.getUserMedia(
94+
requiredMedia
95+
);
96+
mediaStream.current = stream;
9197
}
92-
93-
const stream = await window.navigator.mediaDevices.getUserMedia(
94-
requiredMedia
95-
);
96-
mediaStream.current = stream;
9798
setStatus("idle");
9899
} catch (error) {
99100
setError(error.name);
@@ -224,6 +225,9 @@ export const ReactMediaRecorder = ({
224225
stopRecording,
225226
mediaBlobUrl,
226227
status,
227-
isAudioMuted
228+
isAudioMuted,
229+
previewStream: mediaStream.current
230+
? new MediaStream(mediaStream.current.getVideoTracks())
231+
: null
228232
});
229233
};

0 commit comments

Comments
 (0)