flutter video_thumbnail Work with isolate

73 views Asked by At

I am trying to use the video_thumbnail library inside the Isolae, when it fails to get a thumbnail, an error occurs that cancels the isolate work without giving any output , and the FutureBuilder remains in the waiting state and does not go to the error state or to any other state.

error

and this is my code

 void videoThumpIsolateMain(IsolateParameters isolateParameters) async {
  print(isolateParameters.file.path);
  if (isolateParameters.rootIsolateToken != null) {
    BackgroundIsolateBinaryMessenger.ensureInitialized(
        isolateParameters.rootIsolateToken!);
    try {
      Directory vidDirectory = await DirectoryInTemporary.createVideosImageDirectory();
      String data = await VideoInfo.basicThumbnail(
        videoPath: isolateParameters.file.path,
        videoName: isolateParameters.file.name,
        directory: vidDirectory,
      ) ?? 'error';

      isolateParameters.sendPort.send(data);
      Isolate.exit();
    } catch (e) {
      debugPrint(e.toString());
      isolateParameters.sendPort.send('error');
      Isolate.exit();
    }
  } else {
    isolateParameters.sendPort.send('error');
    Isolate.exit();
  }
}


Future<String> getThumpDataInIsolate({
  required context,
  required FileElement file,
  required List<FileElement> refList,
}) async {
  ReceivePort receivePort = ReceivePort('thump');
  try {
    void Function(IsolateParameters) entryPoint;
    if (file.fileType == FileType.video) {
      entryPoint = videoThumpIsolateMain;
    } else {
      entryPoint = imageThumpIsolateMain;
    }
    await Isolate.spawn(
        entryPoint,
        onError: receivePort.sendPort,
        IsolateParameters(
            rootIsolateToken: RootIsolateToken.instance,
            sendPort: receivePort.sendPort,
            refList: refList,
            file: file));
    dynamic outPut = await receivePort.first;
    if (outPut is String) {
      file.imagePath = outPut;
      return outPut;
    } else {
      file.imagePath = 'error';
      return 'error';
    }
  } catch (e) {
    debugPrint(e.toString());
    file.imagePath = 'error';
    return 'error';
  }
}

and this is basicThumbnail frome video_thumbnail package

static Future<String?> basicThumbnail({
    required String videoPath,
    required String videoName,
    required Directory directory,
  }) async {
    String thumbnailFilePath =
        thumbnailPath(videoName: videoName, directory: directory);
    bool thumbnailFileExists = await File(thumbnailFilePath).exists();
    if (thumbnailFileExists) {
      return thumbnailFilePath;
    } else {
      try{
        String? thumbnail = await VideoThumbnail.thumbnailFile(
          timeMs: 500,
          video: videoPath,
          thumbnailPath: directory.path,
          imageFormat: ImageFormat.JPEG,
          quality: 50,
        );
        if(thumbnail != null && thumbnail.isNotEmpty) {
          return thumbnail;
        }else{
          throw('error');
        }
      }catch(e){
        throw('error $e');
      }

    }
  }

I tried using video_compress inside isolatie as an alternative but it doesn't work inside isolate.

0

There are 0 answers