Is there anyway to access native camera but only video recording in Flutter?

Viewed 27

I'm developing my first app. I just want to record video for my app. The plugins that are available captures both pictures and record videos.

1 Answers

Just use the camera plugin and you're good to go. It shows you the camera preview and it's a matter of Stacking it with your own record button.

Also, if you want to save it to the default gallery app use the gallery_saver plugin.

Idle Recording Galley video Gallery preview
Start recording Stop recording Gallery file video Gallery video preview

Here's the code adapted from the camera plugin example

import 'dart:io';

import 'package:camera/camera.dart';
import 'package:flutter/material.dart';
import 'package:gallery_saver/gallery_saver.dart';

late List<CameraDescription> _cameras;

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();

  _cameras = await availableCameras();
  runApp(const CameraApp());
}

/// CameraApp is the Main Application.
class CameraApp extends StatefulWidget {
  /// Default Constructor
  const CameraApp({Key? key}) : super(key: key);

  @override
  State<CameraApp> createState() => _CameraAppState();
}

class _CameraAppState extends State<CameraApp> {
  late CameraController controller;
  var isRecording = false;

  @override
  void initState() {
    super.initState();
    controller = CameraController(_cameras[0], ResolutionPreset.max);
    controller.initialize().then((_) {
      if (!mounted) {
        return;
      }
      setState(() {});
    }).catchError((Object e) {
      if (e is CameraException) {
        switch (e.code) {
          case 'CameraAccessDenied':
            print('User denied camera access.');
            break;
          default:
            print('Handle other errors.');
            break;
        }
      }
    });
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    if (!controller.value.isInitialized) {
      return Container();
    }
    return MaterialApp(
      home: Stack(children: [
        CameraPreview(controller),
        Positioned(
          bottom: 12,
          left: 0,
          right: 0,
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              if (!isRecording) ElevatedButton.icon(
                onPressed: () {
                  setState(() => isRecording = true);
                  controller.startVideoRecording();
                },
                icon: const Icon(Icons.camera),
                label: const Text('Record'),
              ),
              if (isRecording) ElevatedButton.icon(
                onPressed: () async {
                  setState(() => isRecording = false);
                  final video = await controller.stopVideoRecording();
                  await GallerySaver.saveVideo(video.path);
                  File(video.path).deleteSync();
                },
                icon: const Icon(Icons.stop),
                label: const Text('Stop'),
              ),
            ],
          ),
        )
      ]),
    );
  }
}

Add xmlns:tools and uses-sdk to android/app/src/main/AndroidManifest.xml to make it work on the Android Emulator:

<manifest xmlns:tools="http://schemas.android.com/tools" ...>
    <uses-sdk tools:overrideLibrary="carnegietechnologies.gallery_saver,io.flutter.plugins.camera"/>
   ...

And also android.permission.WRITE_EXTERNAL_STORAGE to android/app/src/profile/AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.flutter_camera">
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
</manifest>
Related