@@ -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 (
@@ -146,7 +145,6 @@ class _MyAppState extends State<MyApp> {
146145 _cameraIndex = 0 ;
147146 _previewSize = null ;
148147 _recording = false ;
149- _recordingTimed = false ;
150148 _cameraInfo =
151149 'Failed to initialize camera: ${e .code }: ${e .description }' ;
152150 });
@@ -165,7 +163,6 @@ class _MyAppState extends State<MyApp> {
165163 _cameraId = - 1 ;
166164 _previewSize = null ;
167165 _recording = false ;
168- _recordingTimed = false ;
169166 _previewPaused = false ;
170167 _cameraInfo = 'Camera disposed' ;
171168 });
@@ -190,56 +187,22 @@ class _MyAppState extends State<MyApp> {
190187 _showInSnackBar ('Picture captured to: ${file .path }' );
191188 }
192189
193- Future <void > _recordTimed (int seconds) async {
194- if (_initialized && _cameraId > 0 && ! _recordingTimed) {
195- unawaited (
196- CameraPlatform .instance.onVideoRecordedEvent (_cameraId).first.then ((
197- VideoRecordedEvent event,
198- ) async {
199- if (mounted) {
200- setState (() {
201- _recordingTimed = false ;
202- });
203-
204- _showInSnackBar ('Video captured to: ${event .file .path }' );
205- }
206- }),
207- );
208-
209- await CameraPlatform .instance.startVideoRecording (
210- _cameraId,
211- maxVideoDuration: Duration (seconds: seconds),
212- );
213-
214- if (mounted) {
215- setState (() {
216- _recordingTimed = true ;
217- });
218- }
219- }
220- }
221-
222190 Future <void > _toggleRecord () async {
223191 if (_initialized && _cameraId > 0 ) {
224- if (_recordingTimed) {
225- /// Request to stop timed recording short.
226- await CameraPlatform .instance.stopVideoRecording (_cameraId);
192+ if (! _recording) {
193+ await CameraPlatform .instance.startVideoRecording (_cameraId);
227194 } else {
228- if (! _recording) {
229- await CameraPlatform .instance.startVideoRecording (_cameraId);
230- } else {
231- final XFile file = await CameraPlatform .instance.stopVideoRecording (
232- _cameraId,
233- );
195+ final XFile file = await CameraPlatform .instance.stopVideoRecording (
196+ _cameraId,
197+ );
234198
235- _showInSnackBar ('Video captured to: ${file .path }' );
236- }
199+ _showInSnackBar ('Video captured to: ${file .path }' );
200+ }
237201
238- if (mounted) {
239- setState (() {
240- _recording = ! _recording;
241- });
242- }
202+ if (mounted) {
203+ setState (() {
204+ _recording = ! _recording;
205+ });
243206 }
244207 }
245208 }
@@ -407,19 +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:
419- (_initialized && ! _recording && ! _recordingTimed)
420- ? () => _recordTimed (5 )
421- : null ,
422- child: const Text ('Record 5 seconds' ),
373+ child: Text (_recording ? 'Stop recording' : 'Record Video' ),
423374 ),
424375 if (_cameras.length > 1 ) ...< Widget > [
425376 const SizedBox (width: 5 ),
0 commit comments