react-native-vision-camera icon indicating copy to clipboard operation
react-native-vision-camera copied to clipboard

✨ isRecording() prop for cameraRef

Open ChristopherGabba opened this issue 1 year ago • 3 comments

What feature or enhancement are you suggesting?

Right now, you can use:

- cameraRef.current?.startRecording() - cameraRef.current?.stopRecording() - cameraRef.current?.pauseRecording() - cameraRef.current?.resumeRecording()

If you call stopRecording() twice, you get a console.warn that you called it twice.

Would be awesome to be able to control this logic within code like so:

if(cameraRef.current?.isRecording()) { // could also be cameraRef.current?.isRecordingAsync() possibly
    cameraRef.current?.stopRecording() // prevent the warning and also get the status of the camera.
}

What Platforms whould this feature/enhancement affect?

iOS, Android

Alternatives/Workarounds

Right now, you just set a separate state variable when you call:

    cameraRef.current.startRecording({
        flash,
        fileType: "mp4",
        onRecordingError: (error) => {
          console.error("Recording failed!", error)
        },
        onRecordingFinished: (video) => {
          console.log(`Recording successfully finished! ${video.path}`)
          onMediaCaptured(video, "video")
          setCameraRecording(false) // track current state of camera in separate variable
        },
    })
    setCameraRecording(true) // track current state of camera in separate variable

Additional information

ChristopherGabba avatar Feb 18 '24 19:02 ChristopherGabba

Good feature request, but I think the answer would be the API I mentioned in this comment: https://github.com/mrousavy/react-native-vision-camera/issues/1319#issuecomment-1741728183

So something like

const recorder = await camera.current.prepareRecorder()

console.log(recorder.state) // <-- 'idle'
await recorder.start()
console.log(recorder.state) // <-- 'recording'
await recorder.pause()
console.log(recorder.state) // <-- 'recording-paused'
await recorder.stop()
console.log(recorder.state) // <-- 'finished' (or idle again?)

Also, in your code:

setCameraRecording(true) // track current state of camera in separate variable

If you don't display this to the user, it would be better to use a useRef for this (state only immutably updates callbacks)

mrousavy avatar Feb 19 '24 12:02 mrousavy

@mrousavy that would work great too! Thanks again

ChristopherGabba avatar Feb 19 '24 12:02 ChristopherGabba

Yea but I'm not gonna work on this for a while, maybe some point in the future. If you need this we can of course build this feature for you through my consultancy company, otherwise I'll maybe take a look at this when VisionCamera is a TurboModule :)

mrousavy avatar Feb 19 '24 12:02 mrousavy