prepareSubtitles method

  1. @override
Future<List<SubtitleItem>> prepareSubtitles(
  1. {required AppModel appModel,
  2. required WidgetRef ref,
  3. required MediaItem item}
)
override

Get the player controller to be used when a media item is loaded up,

Implementation

@override
Future<List<SubtitleItem>> prepareSubtitles({
  required AppModel appModel,
  required WidgetRef ref,
  required MediaItem item,
}) async {
  List<SubtitleItem> items = [];

  File videoFile = File(item.mediaIdentifier);
  Directory directory = Directory(path.dirname(videoFile.path));

  List<FileSystemEntity> entityList = directory.listSync();

  String videoFileBasename = path.basenameWithoutExtension(videoFile.path);

  List<FileSystemEntity> matchingEntities = entityList.where((entity) {
    return entity is File &&
        path.basename(entity.path).startsWith(videoFileBasename) &&
        (path.extension(entity.path).toLowerCase() == '.ass' ||
            path.extension(entity.path).toLowerCase() == '.srt') &&
        videoFile.path != entity.path;
  }).toList();

  for (FileSystemEntity file in matchingEntities) {
    if (file is File) {
      String metadata =
          path.basename(file.path).replaceAll(videoFileBasename, '');
      SubtitleItem? item = await SubtitleUtils.subtitlesFromFile(
        file: file,
        metadata: metadata,
        type: SubtitleItemType.externalSubtitle,
      );
      items.add(item);
    }
  }

  return items;
}