0

I'm a beginner at React Native.

I am trying to access a native(built-in) camera app on Android device.

I used React-Native-Image-Picker to open the camera app but I would like to record a video somehow automatically(?) I mean not using my finger. I need codes that make it to record and stop the video. (I don't mean to give me a code rather, please advise if it is even possible?)

Any help would be very appreciated.

Thank you!

seol hyeon
  • 31
  • 2

1 Answers1

0

It is possible.

Package: https://github.com/mrousavy/react-native-vision-camera

Review the API and Guide section to see how to start and stop recording programmatically.

They also show an example app that demonstrates different types of capture including video recording, ref: https://github.com/mrousavy/react-native-vision-camera/blob/28fc6a68a5744efc85b532a338e2ab1bc8fa45fe/example/src/views/CaptureButton.tsx

    ...
    const onStoppedRecording = useCallback(() => {
    isRecording.current = false;
    cancelAnimation(recordingProgress);
    console.log('stopped recording video!');
  }, [recordingProgress]);
  const stopRecording = useCallback(async () => {
    try {
      if (camera.current == null) throw new Error('Camera ref is null!');

      console.log('calling stopRecording()...');
      await camera.current.stopRecording();
      console.log('called stopRecording()!');
    } catch (e) {
      console.error('failed to stop recording!', e);
    }
  }, [camera]);


  const startRecording = useCallback(() => {
    try {
      if (camera.current == null) throw new Error('Camera ref is null!');

      console.log('calling startRecording()...');
      camera.current.startRecording({
        flash: flash,
        onRecordingError: (error) => {
          console.error('Recording failed!', error);
          onStoppedRecording();
        },
        onRecordingFinished: (video) => {
          console.log(`Recording successfully finished! ${video.path}`);
          onMediaCaptured(video, 'video');
          onStoppedRecording();
        },
      });
      // TODO: wait until startRecording returns to actually find out if the recording has successfully started
      console.log('called startRecording()!');
      isRecording.current = true;
    } catch (e) {
      console.error('failed to start recording!', e, 'camera');
    }
  }, [camera, flash, onMediaCaptured, onStoppedRecording]);
  //#endregion
  ...
Aleksandar Zoric
  • 1,343
  • 3
  • 18
  • 45
  • I installed 'react-native-vision-camera'. An error called Camera: Native Module 'CameraView' was null! Did you run pod install? So I did npx pod-install There is no change. There is the same error. – seol hyeon Jan 25 '22 at 11:53
  • See guide how to setup the env. , ref: https://mrousavy.com/react-native-vision-camera/docs/guides/ – Aleksandar Zoric Jan 25 '22 at 12:34