iOS Development

android – Digicam video recording freezes on beginning the video for the primary time in Flutter

Spread the love


Following are my specs:

Laptop: Macbook M1;
Cellphone: iPhone 13 Professional, iOS 17.1.2;
Flutter: 3.16.4

Bundle: digital camera: ^0.10.5+9

I’m making an attempt to report a video, however with the recent set up the primary time the video hangs/freezes between 1 to 10 seconds, earlier than streaming correctly. However, going again to the earlier display and coming again once more doesn’t create an issue. Identical is the case with going to the following display and coming again to the digital camera display.

Following are the steps that I’m taking to succeed in the video display

  1. Contemporary launch the app
  2. Go to Splash Display
  3. Go to Middleman Display
  4. Go to Open Digicam Display
  5. Web page opens and asks for digital camera and microphone permissions
  6. Faucet the Begin Video Recording button
  7. Display freezes for a while

Following is my code:

import 'dart:async';
import 'dart:io';
import 'bundle:digital camera/digital camera.dart';
import 'bundle:flutter/materials.dart';


@RoutePage()
class CameraPage extends StatefulWidget {
  
  const CameraPage(
      {Key? key})
      : tremendous(key: key);

  @override
  State<CameraPage> createState() => _CameraPageState();
}

class _CameraPageState extends State<CameraPage> {
  bool isRecording = false;
  bool isCameraLoaded = false;
  Checklist<CameraDescription>? cameras;
  late CameraController _cameraController;

  String timeString = "00:00";
  Stopwatch stopwatch = Stopwatch();
  Timer? timer;

  void startTimer() {
    stopwatch.begin();
    timer = Timer.periodic(Length(seconds: 1), replace);
  }

  void replace(Timer t) {
    if (stopwatch.isRunning) {
      setState(
        () {
          timeString = (stopwatch.elapsed.inMinutes % 60)
                  .toString()
                  .padLeft(timerTextPadding, "0") +
              ":" +
              (stopwatch.elapsed.inSeconds % 60)
                  .toString()
                  .padLeft(timerTextPadding, "0");
        },
      );
    }
  }

  void resetTimer() {
    timer!.cancel();
    stopwatch.reset();
    setState(() {
      timeString = "00:00";
    });
    stopwatch.cease();
  }

  void stopTimer() {
    setState(() {
      timer?.cancel();
      stopwatch.cease();
    });
  }

  @override
  void initState() {
    tremendous.initState();
    loadCamera();
  }

  @override
  void dispose() {
    _cameraController.pauseVideoRecording();
    _cameraController.dispose();
    timer!.cancel();
    tremendous.dispose();
  }

  void loadCamera() async {
    ultimate cameras = await availableCameras();
    var digital camera = cameras.first;
    _cameraController =
        CameraController(digital camera, ResolutionPreset.medium, enableAudio: false);

    await _cameraController.initialize().then(
          (_) => {
            if (mounted)
              {
                setState(
                  () {
                    _prepareForVideoRecording();
                  },
                )
              },
          },
        );
    setState(() {
      isCameraLoaded = true;
    });
  }

  _prepareForVideoRecording() async {
    await _cameraController.prepareForVideoRecording();
  }

  _stopVideoRecording() async {
    if (isRecording) {
      setState(() => isRecording = false);
      ultimate videoFile = await _cameraController.stopVideoRecording();
      resetTimer();
      stopTimer();
    }
  }

  _recordVideo() {
    _cameraController.startVideoRecording();
    setState(() => isRecording = true);
    startTimer();
  }

  
  @override
  Widget construct(BuildContext context) {
    ultimate cameraPreviewKey = GlobalKey();
    return Scaffold(
      appBar: AppBar(/*AppBar code*/),
      physique: Column(
        mainAxisAlignment: MainAxisAlignment.begin,
        crossAxisAlignment: CrossAxisAlignment.begin,
        kids: [
          Padding(
            padding: const EdgeInsets.symmetric(horizontal: 0),
            child: (!isCameraLoaded)
                ? Container(
                    width: MediaQuery.of(context).size.width,
                    height: MediaQuery.of(context).size.width / 3 * 4,
                    child: Center(
                      child: const CircularProgressIndicator(),
                    ),
                  )
                : Row(
                    children: [
                      Expanded(
                        child: Container(
                          width: MediaQuery.of(context).size.width,
                          height: MediaQuery.of(context).size.width / 3 * 4,
                          child: CameraPreview(
                            _cameraController,
                            key: cameraPreviewKey,
                            child: Container(
                              child: Padding(
                                padding: EdgeInsets.all(Sizes.x8),
                                child: Container(
                                  width: MediaQuery.of(context).size.width,
                                  height:
                                      MediaQuery.of(context).size.width / 3 * 4,
                                  decoration: BoxDecoration(
                                    border: Border.all(
                                      width: borderWidth,
                                    ),
                                  ),
                                ),
                              ),
                            ),
                          ),
                        ),
                      ),
                    ],
                  ),
          ),
          
          const Spacer(),
          SizedBox(
            top: Sizes.x16,
          ),
          Padding(
            padding: const EdgeInsets.symmetric(horizontal: Sizes.x16),
            youngster: Middle(
              youngster: Column(
                kids: [
                  isRecording
                      ? GestureDetector(
                          child: stopButtonWidget(context),
                          onTap: () {
                            _stopVideoRecording();
                          },
                        )
                      : GestureDetector(
                          child: playButtonWidget(context),
                          onTap: () {
                            _recordVideo();
                          },
                        ),
                  SizedBox(
                    height: Sizes.x16,
                  ),
                  isRecording
                      ? FittedBox(
                          fit: BoxFit.fitWidth,
                          child: Text(
                            l10n.vide_record_screen_end,
                          ),
                        )
                      : FittedBox(
                          fit: BoxFit.fitWidth,
                          child: Text(
                            l10n.vide_record_screen_start,
                            style: Theme.of(context).Style
                          ),
                        ),
                  SizedBox(
                    height: Sizes.x24,
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}

Leave a Reply

Your email address will not be published. Required fields are marked *