Skip to content

Instantly share code, notes, and snippets.

@tatsuyasusukida
Created September 16, 2022 02:04
Show Gist options
  • Save tatsuyasusukida/667a488796472c9e613b758254bfab50 to your computer and use it in GitHub Desktop.
Save tatsuyasusukida/667a488796472c9e613b758254bfab50 to your computer and use it in GitHub Desktop.
Flutter video recording example
import 'package:camera/camera.dart';
import 'package:flutter/material.dart';
import 'video_recorder_screen.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
final cameras = await availableCameras();
final firstCamera = cameras.firstWhere((camera) {
return camera.lensDirection == CameraLensDirection.front;
});
runApp(MaterialApp(
theme: ThemeData.dark(),
home: VideoRecorderScreen(camera: firstCamera),
));
}
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';
class VideoPlayerScreen extends StatefulWidget {
final String videoPath;
const VideoPlayerScreen({super.key, required this.videoPath});
@override
State<VideoPlayerScreen> createState() => _VideoPlayerScreenState();
}
class _VideoPlayerScreenState extends State<VideoPlayerScreen> {
late VideoPlayerController _controller;
late Future<void> _initializeControllerFuture;
@override
void initState() {
super.initState();
_controller = VideoPlayerController.file(File(widget.videoPath));
_initializeControllerFuture = _controller.initialize();
_controller.setLooping(true);
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Video player screen')),
body: FutureBuilder(
future: _initializeControllerFuture,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
return AspectRatio(
aspectRatio: _controller.value.aspectRatio,
child: VideoPlayer(_controller),
);
} else {
return const Center(
child: CircularProgressIndicator(),
);
}
},
),
floatingActionButton: FloatingActionButton(
onPressed: () {
setState(() {
if (_controller.value.isPlaying) {
_controller.pause();
} else {
_controller.play();
}
});
},
child: Icon(
_controller.value.isPlaying ? Icons.pause : Icons.play_arrow,
),
),
);
}
}
import 'package:camera/camera.dart';
import 'package:flutter/material.dart';
import 'video_player_screen.dart';
class VideoRecorderScreen extends StatefulWidget {
final CameraDescription camera;
const VideoRecorderScreen({
super.key,
required this.camera,
});
@override
State<VideoRecorderScreen> createState() => _VideoRecorderScreenState();
}
class _VideoRecorderScreenState extends State<VideoRecorderScreen> {
late CameraController _controller;
late Future<void> _initializeControllerFuture;
bool _isRecording = false;
@override
void initState() {
super.initState();
_controller = CameraController(
widget.camera,
ResolutionPreset.medium,
);
_initializeControllerFuture = _controller.initialize();
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Video recorder screen')),
body: FutureBuilder<void>(
future: _initializeControllerFuture,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
return CameraPreview(_controller);
} else {
return const Center(child: CircularProgressIndicator());
}
},
),
floatingActionButton: FloatingActionButton(
onPressed: () async {
try {
await _initializeControllerFuture;
if (!mounted) {
return;
}
if (_isRecording) {
final video = await _controller.stopVideoRecording();
await Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => VideoPlayerScreen(
videoPath: video.path,
),
),
);
} else {
await _controller.prepareForVideoRecording();
await _controller.startVideoRecording();
}
setState(() {
_isRecording = !_isRecording;
});
} catch (e) {
print(e);
}
},
child: Icon(_isRecording ? Icons.stop : Icons.circle),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment