@@ -21,8 +21,11 @@ export type ReactMediaRecorderHookProps = {
21
21
video ?: boolean | MediaTrackConstraints ;
22
22
screen ?: boolean ;
23
23
onStop ?: ( blobUrl : string , blob : Blob ) => void ;
24
+ onStart ?: ( ) => void ;
24
25
blobPropertyBag ?: BlobPropertyBag ;
25
26
mediaRecorderOptions ?: MediaRecorderOptions | null ;
27
+ customMediaStream ?: MediaStream | null ;
28
+ stopStreamsOnStop ?: boolean ;
26
29
askPermissionOnMount ?: boolean ;
27
30
} ;
28
31
export type ReactMediaRecorderProps = ReactMediaRecorderHookProps & {
@@ -60,9 +63,12 @@ export function useReactMediaRecorder({
60
63
audio = true ,
61
64
video = false ,
62
65
onStop = ( ) => null ,
66
+ onStart = ( ) => null ,
63
67
blobPropertyBag,
64
68
screen = false ,
65
69
mediaRecorderOptions = null ,
70
+ customMediaStream = null ,
71
+ stopStreamsOnStop = true ,
66
72
askPermissionOnMount = false ,
67
73
} : ReactMediaRecorderHookProps ) : ReactMediaRecorderRenderProps {
68
74
const mediaRecorder = useRef < MediaRecorder | null > ( null ) ;
@@ -80,7 +86,9 @@ export function useReactMediaRecorder({
80
86
video : typeof video === "boolean" ? ! ! video : video ,
81
87
} ;
82
88
try {
83
- if ( screen ) {
89
+ if ( customMediaStream ) {
90
+ mediaStream . current = customMediaStream ;
91
+ } else if ( screen ) {
84
92
//@ts -ignore
85
93
const stream = ( await window . navigator . mediaDevices . getDisplayMedia ( {
86
94
video : video || true ,
@@ -196,6 +204,7 @@ export function useReactMediaRecorder({
196
204
mediaRecorder . current = new MediaRecorder ( mediaStream . current ) ;
197
205
mediaRecorder . current . ondataavailable = onRecordingActive ;
198
206
mediaRecorder . current . onstop = onRecordingStop ;
207
+ mediaRecorder . current . onstart = onRecordingStart ;
199
208
mediaRecorder . current . onerror = ( ) => {
200
209
setError ( "NO_RECORDER" ) ;
201
210
setStatus ( "idle" ) ;
@@ -209,6 +218,10 @@ export function useReactMediaRecorder({
209
218
mediaChunks . current . push ( data ) ;
210
219
} ;
211
220
221
+ const onRecordingStart = ( ) => {
222
+ onStart ( ) ;
223
+ } ;
224
+
212
225
const onRecordingStop = ( ) => {
213
226
const [ chunk ] = mediaChunks . current ;
214
227
const blobProperty : BlobPropertyBag = Object . assign (
@@ -249,8 +262,10 @@ export function useReactMediaRecorder({
249
262
if ( mediaRecorder . current . state !== "inactive" ) {
250
263
setStatus ( "stopping" ) ;
251
264
mediaRecorder . current . stop ( ) ;
252
- mediaStream . current &&
253
- mediaStream . current . getTracks ( ) . forEach ( ( track ) => track . stop ( ) ) ;
265
+ if ( stopStreamsOnStop ) {
266
+ mediaStream . current &&
267
+ mediaStream . current . getTracks ( ) . forEach ( ( track ) => track . stop ( ) ) ;
268
+ }
254
269
mediaChunks . current = [ ] ;
255
270
}
256
271
}
0 commit comments