Flutter provides camera and video_player packages. The camera is used to access the phoneβs camera, and video_plater is used to record video. You can use the camera package and record video. You can find the code below:
First you must update the pubspec file, for example:
dependencies: camera: ^0.2.9+1 fluttertoast: path_provider: video_player:
fluttertoast is a toast for your camera, and path_provider indicates the path where your video will be saved. Then you should import these packages into your dart file and write your own implementation. You can find the sample code below. A list of available cameras on your phone will open, including external cameras, so you can select any one to record video.
import 'dart:async'; import 'dart:io'; import 'package:camera/camera.dart'; import 'package:flutter/material.dart'; import 'package:path_provider/path_provider.dart'; import 'package:video_player/video_player.dart'; import 'package:fluttertoast/fluttertoast.dart'; class VideoRecorderExample extends StatefulWidget { @override _VideoRecorderExampleState createState() { return _VideoRecorderExampleState(); } } class _VideoRecorderExampleState extends State<VideoRecorderExample> { CameraController controller; String videoPath; List<CameraDescription> cameras; int selectedCameraIdx; final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>(); @override void initState() { super.initState(); // Get the listonNewCameraSelected of available cameras. // Then set the first camera as selected. availableCameras() .then((availableCameras) { cameras = availableCameras; if (cameras.length > 0) { setState(() { selectedCameraIdx = 0; }); _onCameraSwitched(cameras[selectedCameraIdx]).then((void v) {}); } }) .catchError((err) { print('Error: $err.code\nError Message: $err.message'); }); } @override Widget build(BuildContext context) { return Scaffold( key: _scaffoldKey, appBar: AppBar( title: const Text('Camera example'), ), body: Column( children: <Widget>[ Expanded( child: Container( child: Padding( padding: const EdgeInsets.all(1.0), child: Center( child: _cameraPreviewWidget(), ), ), decoration: BoxDecoration( color: Colors.black, border: Border.all( color: controller != null && controller.value.isRecordingVideo ? Colors.redAccent : Colors.grey, width: 3.0, ), ), ), ), Padding( padding: const EdgeInsets.all(5.0), child: Row( mainAxisAlignment: MainAxisAlignment.start, children: <Widget>[ _cameraTogglesRowWidget(), _captureControlRowWidget(), Expanded( child: SizedBox(), ), ], ), ), ], ), ); } IconData _getCameraLensIcon(CameraLensDirection direction) { switch (direction) { case CameraLensDirection.back: return Icons.camera_rear; case CameraLensDirection.front: return Icons.camera_front; case CameraLensDirection.external: return Icons.camera; default: return Icons.device_unknown; } } // Display 'Loading' text when the camera is still loading. Widget _cameraPreviewWidget() { if (controller == null || !controller.value.isInitialized) { return const Text( 'Loading', style: TextStyle( color: Colors.white, fontSize: 20.0, fontWeight: FontWeight.w900, ), ); } return AspectRatio( aspectRatio: controller.value.aspectRatio, child: CameraPreview(controller), ); } /// Display a row of toggle to select the camera (or a message if no camera is available). Widget _cameraTogglesRowWidget() { if (cameras == null) { return Row(); } CameraDescription selectedCamera = cameras[selectedCameraIdx]; CameraLensDirection lensDirection = selectedCamera.lensDirection; return Expanded( child: Align( alignment: Alignment.centerLeft, child: FlatButton.icon( onPressed: _onSwitchCamera, icon: Icon( _getCameraLensIcon(lensDirection) ), label: Text("${lensDirection.toString() .substring(lensDirection.toString().indexOf('.')+1)}") ), ), ); } /// Display the control bar with buttons to record videos. Widget _captureControlRowWidget() { return Expanded( child: Align( alignment: Alignment.center, child: Row( mainAxisAlignment: MainAxisAlignment.spaceEvenly, mainAxisSize: MainAxisSize.max, children: <Widget>[ IconButton( icon: const Icon(Icons.videocam), color: Colors.blue, onPressed: controller != null && controller.value.isInitialized && !controller.value.isRecordingVideo ? _onRecordButtonPressed : null, ), IconButton( icon: const Icon(Icons.stop), color: Colors.red, onPressed: controller != null && controller.value.isInitialized && controller.value.isRecordingVideo ? _onStopButtonPressed : null, ) ], ), ), ); } String timestamp() => DateTime.now().millisecondsSinceEpoch.toString(); Future<void> _onCameraSwitched(CameraDescription cameraDescription) async { if (controller != null) { await controller.dispose(); } controller = CameraController(cameraDescription, ResolutionPreset.high);