generateThumbnail2 method

dynamic generateThumbnail2(
  1. File video, {
  2. Function? checkandgo,
})

Implementation

generateThumbnail2(File video, {Function? checkandgo}) async {
  if (Platform.isIOS) {
    var ttt = await VideoThumbnail.thumbnailFile(
        video: video.path, imageFormat: ImageFormat.JPEG);
    File fi = File(ttt.path);
    fileThumbanil = fi;
    setState(() {});
    if (checkandgo != null) {
      checkandgo.call();
    }
  } else {
    final String _videoPath = video.path;

    double _eachPart = _videoDuration / _numberOfThumbnails;

    List<Uint8List> _byteList = [];
    // the cache of last thumbnail
    Uint8List _lastBytes;

    for (int i = 1; i <= _numberOfThumbnails; i++) {
      Uint8List? _bytes;
      _bytes = await VideoThumbnail.thumbnailData(
        video: _videoPath,
        imageFormat: ImageFormat.JPEG,
        timeMs: (_eachPart * i).toInt(),
        quality: 75,
      );

      // if current thumbnail is null use the last thumbnail
      if (_bytes != null) {
        _lastBytes = _bytes;
        _byteList.add(_bytes);
      } else {
        // _bytes = _lastBytes;
        // _byteList.add(_bytes);
      }
    }

    if (_byteList.length > 0) {
      final tempDir = await getTemporaryDirectory();
      await File(
              '${tempDir.path}${DateTime.now().toString()}-${DateTime.now().microsecondsSinceEpoch}.png')
          .create()
          .then((value2) {
        value2.writeAsBytesSync(_byteList.first);

        fileThumbanil = value2;
        setState(() {});
        if (checkandgo != null) {
          checkandgo.call();
        }
      });
    }
  }
}