Skip to content

Instantly share code, notes, and snippets.

@Alvarocda
Last active March 17, 2022 12:33
Show Gist options
  • Save Alvarocda/60231a63817659fd9d5d2a23578a4881 to your computer and use it in GitHub Desktop.
Save Alvarocda/60231a63817659fd9d5d2a23578a4881 to your computer and use it in GitHub Desktop.
Minimal reproducible code
import 'package:camera/camera.dart';
import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';
///
///
///
class FlutterTest extends StatefulWidget {
///
///
///
const FlutterTest({Key? key}) : super(key: key);
///
///
///
@override
State<FlutterTest> createState() => _FlutterTestState();
}
///
///
///
class _FlutterTestState extends State<FlutterTest> {
CameraController? _cameraController;
List<CameraDescription>? _availableCameras;
final ValueNotifier<VideoPlayerController?> videoPlayerController =
ValueNotifier<VideoPlayerController?>(null);
///
///
///
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Center(
child: ElevatedButton(
onPressed: recordVideo,
child: Text('Record video'),
),
),
ValueListenableBuilder<VideoPlayerController?>(
valueListenable: videoPlayerController,
builder: (BuildContext context, VideoPlayerController? value,
Widget? child) {
if (value == null) {
return Text('Please, record a video');
}
return VideoPlayer(value);
},
),
],
),
);
}
///
///
///
Future<void> recordVideo() async {
_availableCameras ??= await availableCameras();
_cameraController ??= CameraController(
_availableCameras!.first,
ResolutionPreset.max,
);
await _cameraController!.initialize();
await _cameraController!.startVideoRecording();
await Future<void>.delayed(Duration(seconds: 10));
XFile xfile = await _cameraController!.stopVideoRecording();
VideoPlayerController playerController =
VideoPlayerController.network(xfile.path);
/// This is where the crash happens
await playerController.initialize();
videoPlayerController.value = playerController;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment