skip to Main Content

I am using the camera_awesome package in Flutter to capture photos and videos. I want to set a limit on video recording so that it automatically stops after 30 seconds.

Currently, my implementation does not include this functionality. Could someone guide me on how to achieve this? Here is my existing code:

import 'dart:io';
import 'package:camerawesome/camerawesome_plugin.dart';
import 'package:camerawesome/pigeon.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:open_file/open_file.dart';
import 'package:path_provider/path_provider.dart';
import 'package:tymelyne/AddPosts/MediaBank/MainMediaBankScreen.dart';
import 'package:tymelyne/AddPosts/MediaBank/MediaBankFullScreen.dart';
import 'package:tymelyne/Controllers/AuthDBController.dart';
import 'package:tymelyne/Universal/Widgets/UniversalWidgets.dart';
import '../Universal/Controllers/PersistController.dart';

// Instances
final _universalWidgets = UniversalWidgets();
final _persistControllerUNI = Get.find<PersistControllerUNI>();

//
class MyCameraAwesome extends StatelessWidget {
  const MyCameraAwesome({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: CameraAwesomeBuilder.awesome(
        // Enable the volume buttons to take pictures or record videos
        enablePhysicalButton: true,

        //
        progressIndicator: Center(
          child: _universalWidgets.customProgressIndicator(),
        ),

        //
        availableFilters: awesomePresetFiltersList,

        //
        saveConfig: SaveConfig.photoAndVideo(
          initialCaptureMode: CaptureMode.photo,
          mirrorFrontCamera: true,

          photoPathBuilder: (sensors) async {
            final Directory extDir = await getApplicationDocumentsDirectory();
            final testDir = await Directory(
              '${extDir.path}/xyzapp',
            ).create(recursive: true);

            //
            if (sensors.length == 1) {
              final String filePath =
                  '${testDir.path}/${DateTime.now().millisecondsSinceEpoch}.jpg';

              //
              _persistControllerUNI.updateAndPersistMediaBank(
                filePath: filePath,
                fileType: 'image',
              );

              return SingleCaptureRequest(filePath, sensors.first);
            } else {
              // Separate pictures taken with front and back camera
              return MultipleCaptureRequest(
                {
                  for (final sensor in sensors)
                    sensor:
                        '${testDir.path}/${sensor.position == SensorPosition.front ? 'front_' : "back_"}${DateTime.now().millisecondsSinceEpoch}.jpg',
                },
              );
            }
          },

          //
          videoPathBuilder: (sensors) async {
            final Directory extDir = await getApplicationDocumentsDirectory();
            final testDir = await Directory(
              '${extDir.path}/xyzapp',
            ).create(recursive: true);
            if (sensors.length == 1) {
              final String filePath =
                  '${testDir.path}/${DateTime.now().millisecondsSinceEpoch}.mp4';

              //
              _persistControllerUNI.updateAndPersistMediaBank(
                filePath: filePath,
                fileType: 'video',
              );
              return SingleCaptureRequest(filePath, sensors.first);
            } else {
              // Separate pictures taken with front and back camera
              return MultipleCaptureRequest(
                {
                  for (final sensor in sensors)
                    sensor:
                        '${testDir.path}/${sensor.position == SensorPosition.front ? 'front_' : "back_"}${DateTime.now().millisecondsSinceEpoch}.jpg',
                },
              );
            }
          },

          videoOptions: VideoOptions(
            enableAudio: true,
            quality: VideoRecordingQuality.highest,
          ),

          exifPreferences: ExifPreferences(
            saveGPSLocation: false,
          ),
        ),

        //
        sensorConfig: SensorConfig.single(
          sensor: Sensor.position(SensorPosition.back),
          flashMode: FlashMode.none,
          aspectRatio: CameraAspectRatios.ratio_16_9,
          zoom: 0.0,
        ),

        //
        onMediaTap: (mediaCapture) {
          //
          mediaCapture.captureRequest.when(
            single: (single) async {
              if (single.file != null) {
                Navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (context) => const MainMediaBankScreen(),
                  ),
                );

                //
                Navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (context) => const MediaBankFullScreen(
                      initialPageIndex: 0,
                    ),
                  ),
                );
              }
            },
          );

          //
          _persistControllerUNI.updateIsLandscapeModeMediaBank();
        },

        //
        topActionsBuilder: (state) {
          return AwesomeTopActions(
            state: state,
            padding: const EdgeInsets.only(
              left: 10,
              right: 10,
              top: 15,
            ),
            children: [
              //
              IconButton(
                onPressed: () => Navigator.pop(context),
                icon: const Icon(
                  Icons.close,
                ),
              ),

              //
              AwesomeFlashButton(state: state),
            ],
          );
        },
      ),
    );
  }
}

Any help or suggestions on how to set a 30-second limit for video recording would be greatly appreciated.

Thank you in advance!

2

Answers


  1. camera_awesome package does not have that functionality it seems.

    You can fork it and customize it for you requirement and use it or
    You can use image_picker insted which has maxDuration param inbuilt which solves your issue.

    Login or Signup to reply.
  2. I’m not sure but I think you can do something like this:

    
    CameraAwesomeBuilder.custom(
      saveConfig: SaveConfig.photoAndVideo(),
      builder: (cameraState, previewSize, previewRect) {
        // Return your UI (a Widget)
        return cameraState.when(
          onPreparingCamera: (state) => const Center(child: CircularProgressIndicator()),
          onPhotoMode: (state) => TakePhotoUI(state),
          onVideoMode: (state) {
             state.startRecording();
             Future.delayed(Duration(seconds: 30)).then((){state.stopRecording()})
          },
          onVideoRecordingMode: (state) => RecordVideoUI(state, recording: true),
        );
      },
    )
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search