@@ -28,7 +28,6 @@ class _MyAppState extends State<MyApp> {
2828 int _cameraId = - 1 ;
2929 bool _initialized = false ;
3030 bool _recording = false ;
31- bool _recordingTimed = false ;
3231 bool _previewPaused = false ;
3332 Size ? _previewSize;
3433 MediaSettings _mediaSettings = const MediaSettings (
@@ -147,7 +146,6 @@ class _MyAppState extends State<MyApp> {
147146 _cameraIndex = 0 ;
148147 _previewSize = null ;
149148 _recording = false ;
150- _recordingTimed = false ;
151149 _cameraInfo =
152150 'Failed to initialize camera: ${e .code }: ${e .description }' ;
153151 });
@@ -166,7 +164,6 @@ class _MyAppState extends State<MyApp> {
166164 _cameraId = - 1 ;
167165 _previewSize = null ;
168166 _recording = false ;
169- _recordingTimed = false ;
170167 _previewPaused = false ;
171168 _cameraInfo = 'Camera disposed' ;
172169 });
@@ -191,56 +188,22 @@ class _MyAppState extends State<MyApp> {
191188 _showInSnackBar ('Picture captured to: ${file .path }' );
192189 }
193190
194- Future <void > _recordTimed (int seconds) async {
195- if (_initialized && _cameraId > 0 && ! _recordingTimed) {
196- unawaited (
197- CameraPlatform .instance.onVideoRecordedEvent (_cameraId).first.then ((
198- VideoRecordedEvent event,
199- ) async {
200- if (mounted) {
201- setState (() {
202- _recordingTimed = false ;
203- });
204-
205- _showInSnackBar ('Video captured to: ${event .file .path }' );
206- }
207- }),
208- );
209-
210- await CameraPlatform .instance.startVideoRecording (
211- _cameraId,
212- maxVideoDuration: Duration (seconds: seconds),
213- );
214-
215- if (mounted) {
216- setState (() {
217- _recordingTimed = true ;
218- });
219- }
220- }
221- }
222-
223191 Future <void > _toggleRecord () async {
224192 if (_initialized && _cameraId > 0 ) {
225- if (_recordingTimed) {
226- /// Request to stop timed recording short.
227- await CameraPlatform .instance.stopVideoRecording (_cameraId);
193+ if (! _recording) {
194+ await CameraPlatform .instance.startVideoRecording (_cameraId);
228195 } else {
229- if (! _recording) {
230- await CameraPlatform .instance.startVideoRecording (_cameraId);
231- } else {
232- final XFile file = await CameraPlatform .instance.stopVideoRecording (
233- _cameraId,
234- );
196+ final XFile file = await CameraPlatform .instance.stopVideoRecording (
197+ _cameraId,
198+ );
235199
236- _showInSnackBar ('Video captured to: ${file .path }' );
237- }
200+ _showInSnackBar ('Video captured to: ${file .path }' );
201+ }
238202
239- if (mounted) {
240- setState (() {
241- _recording = ! _recording;
242- });
243- }
203+ if (mounted) {
204+ setState (() {
205+ _recording = ! _recording;
206+ });
244207 }
245208 }
246209 }
@@ -407,18 +370,7 @@ class _MyAppState extends State<MyApp> {
407370 const SizedBox (width: 5 ),
408371 ElevatedButton (
409372 onPressed: _initialized ? _toggleRecord : null ,
410- child: Text (
411- (_recording || _recordingTimed)
412- ? 'Stop recording'
413- : 'Record Video' ,
414- ),
415- ),
416- const SizedBox (width: 5 ),
417- ElevatedButton (
418- onPressed: (_initialized && ! _recording && ! _recordingTimed)
419- ? () => _recordTimed (5 )
420- : null ,
421- child: const Text ('Record 5 seconds' ),
373+ child: Text (_recording ? 'Stop recording' : 'Record Video' ),
422374 ),
423375 if (_cameras.length > 1 ) ...< Widget > [
424376 const SizedBox (width: 5 ),
0 commit comments