@@ -21,8 +21,11 @@ export type ReactMediaRecorderHookProps = {
2121 video ?: boolean | MediaTrackConstraints ;
2222 screen ?: boolean ;
2323 onStop ?: ( blobUrl : string , blob : Blob ) => void ;
24+ onStart ?: ( ) => void ;
2425 blobPropertyBag ?: BlobPropertyBag ;
2526 mediaRecorderOptions ?: MediaRecorderOptions | null ;
27+ customMediaStream ?: MediaStream | null ;
28+ stopStreamsOnStop ?: boolean ;
2629 askPermissionOnMount ?: boolean ;
2730} ;
2831export type ReactMediaRecorderProps = ReactMediaRecorderHookProps & {
@@ -60,9 +63,12 @@ export function useReactMediaRecorder({
6063 audio = true ,
6164 video = false ,
6265 onStop = ( ) => null ,
66+ onStart = ( ) => null ,
6367 blobPropertyBag,
6468 screen = false ,
6569 mediaRecorderOptions = null ,
70+ customMediaStream = null ,
71+ stopStreamsOnStop = true ,
6672 askPermissionOnMount = false ,
6773} : ReactMediaRecorderHookProps ) : ReactMediaRecorderRenderProps {
6874 const mediaRecorder = useRef < MediaRecorder | null > ( null ) ;
@@ -80,7 +86,9 @@ export function useReactMediaRecorder({
8086 video : typeof video === "boolean" ? ! ! video : video ,
8187 } ;
8288 try {
83- if ( screen ) {
89+ if ( customMediaStream ) {
90+ mediaStream . current = customMediaStream ;
91+ } else if ( screen ) {
8492 //@ts -ignore
8593 const stream = ( await window . navigator . mediaDevices . getDisplayMedia ( {
8694 video : video || true ,
@@ -196,6 +204,7 @@ export function useReactMediaRecorder({
196204 mediaRecorder . current = new MediaRecorder ( mediaStream . current ) ;
197205 mediaRecorder . current . ondataavailable = onRecordingActive ;
198206 mediaRecorder . current . onstop = onRecordingStop ;
207+ mediaRecorder . current . onstart = onRecordingStart ;
199208 mediaRecorder . current . onerror = ( ) => {
200209 setError ( "NO_RECORDER" ) ;
201210 setStatus ( "idle" ) ;
@@ -209,6 +218,10 @@ export function useReactMediaRecorder({
209218 mediaChunks . current . push ( data ) ;
210219 } ;
211220
221+ const onRecordingStart = ( ) => {
222+ onStart ( ) ;
223+ } ;
224+
212225 const onRecordingStop = ( ) => {
213226 const [ chunk ] = mediaChunks . current ;
214227 const blobProperty : BlobPropertyBag = Object . assign (
@@ -249,8 +262,10 @@ export function useReactMediaRecorder({
249262 if ( mediaRecorder . current . state !== "inactive" ) {
250263 setStatus ( "stopping" ) ;
251264 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+ }
254269 mediaChunks . current = [ ] ;
255270 }
256271 }
0 commit comments