getCloseCaptionFile method

Future<List> getCloseCaptionFile(
  1. dynamic url
)

Implementation

Future<List> getCloseCaptionFile(url) async {
  try {
    final data = await http.get(Uri.parse(url));
    final srtContent = data.body.toString().trim();
    var lines = srtContent.split('\n');
    // var subtitles = <String, String>{};
    // print("---->${lines.length}");
    List<Subtitle> subtitles = [];
    int index = 0;

    for (int i = 0; i < lines.length; i++) {
      final line = lines[i].trim();

      if (line.isEmpty) {
        continue;
      }

      if (int.tryParse(line) != null) {
        index = int.parse(line);
      } else if (line.contains('-->')) {
        final parts = line.split(' --> ');
        final start = parseDuration(parts[0]);
        final end = parseDuration(parts[1]);

        var data = lines[++i].trim();
        var j = i + 1;
        if (lines[j].isNotEmpty) {
          data += " ${lines[++i]}";
        }
        subtitles
            .add(Subtitle(index: index, start: start, end: end, data: data));
        // print("---->${subtitles.length}");
      }
      // print("---->${subtitles.length}");
    }
    return subtitles;
  } catch (e) {
    debugPrint(e.toString());
  }
  return [];
}