Sub topic 15
Recording Video with Flutter’s Camera Plugin
Assume you know Dart async and await, Flutter StatefulWidget lifecycle methods, and how to add packages with pubspec.yaml. This chapter uses the camera package. The camera must already be discovered and initialized before recording starts.
Choose a Recording Approach
Use image_picker when the user should choose an existing video or record one through the system camera interface:
final video = await ImagePicker().pickVideo(
source: ImageSource.camera,
maxDuration: const Duration(minutes: 2),
);Use camera when recording belongs inside your own screen. It provides a live preview, recording controls, flash settings, and camera switching.
The recording flow is:
Start and Stop a Recording
A CameraController records video after initialization. Set enableAudio to true if the video should contain microphone audio. The platform must also grant microphone permission.
The following widget initializes the first available camera and records one video:
import 'package:camera/camera.dart';
import 'package:flutter/material.dart';
class VideoCameraScreen extends StatefulWidget {
const VideoCameraScreen({super.key});
@override
State<VideoCameraScreen> createState() => _VideoCameraScreenState();
}
class _VideoCameraScreenState extends State<VideoCameraScreen> {
CameraController? _controller;
XFile? _recordedVideo;
@override
void initState() {
super.initState();
_initialize();
}
Future<void> _initialize() async {
final cameras = await availableCameras();
final controller = CameraController(
cameras.first,
ResolutionPreset.medium,
enableAudio: true,
);
await controller.initialize();
if (!mounted) {
await controller.dispose();
return;
}
setState(() => _controller = controller);
}
Future<void> _toggleRecording() async {
final controller = _controller;
if (controller == null || !controller.value.isInitialized) return;
if (controller.value.isRecordingVideo) {
final video = await controller.stopVideoRecording();
if (mounted) setState(() => _recordedVideo = video);
} else {
await controller.startVideoRecording();
if (mounted) setState(() {});
}
}
@override
void dispose() {
_controller?.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
final controller = _controller;
if (controller == null || !controller.value.isInitialized) {
return const Scaffold(body: Center(child: CircularProgressIndicator()));
}
final recording = controller.value.isRecordingVideo;
return Scaffold(
body: CameraPreview(controller),
floatingActionButton: FloatingActionButton(
onPressed: _toggleRecording,
child: Icon(recording ? Icons.stop : Icons.videocam),
),
);
}
}startVideoRecording returns after recording begins. stopVideoRecording returns an XFile, which contains the temporary video path. Move or upload that file before treating it as permanent storage.
Permissions and Recording Limits
On iOS, add NSCameraUsageDescription and NSMicrophoneUsageDescription to ios/Runner/Info.plist. Android permissions are normally declared by the camera plugin, but the user can still deny access at runtime.
Consider the device’s storage, resolution, and maximum recording duration. Higher resolutions create larger files and may fail on slower devices. Disable the record button while stopping a video so two stop requests cannot run at once.
Exercise
Which method returns the recorded video, and what type does it return?
Show answer
stopVideoRecording returns a Future<XFile>. Await it, then use the XFile path to preview, copy, or upload the video.