支持 srt/vtt 视频播放器 flutter 中的字幕和同步

support srt/vtt subtitle and syncing in video player flutter

我想在我的应用中播放 srt 字幕。到目前为止,我已经集成了 srt_parser。 另外,我可以通过这段代码获得玩家的当前位置

  inMilliseconds = _videoPlayerController.value.position.inMilliseconds;

这里是srt解析代码。

  String data = '''1
  00:00:01,600 --> 00:00:04,200
  English (US)

  2
  00:00:05,900 --> 00:00:07,999
  This is a subtitle in American English

  3
  00:00:10,000 --> 00:00:14,000
  Adding subtitles is very easy to do''';

  srtParser() {
  List<Subtitle> subtitles = parseSrt(data);
  for (Subtitle item in subtitles) {
  print(
      'inMilliseconds ${inMilliseconds}  Begin is: ${item.range.begin} and End is: 
  ${item.range.end}');
  if (inMilliseconds > item.range.begin &&
      inMilliseconds < item.range.end) {
    for (var line in item.parsedLines) {
      for (var subLine in line.subLines) {
        print(
            'myline${item.parsedLines.indexOf(line)} subline${line.subLines.indexOf(subLine)} is: ${subLine.rawString}');
        setState(() {
          videoSubtitle =
              '${item.parsedLines.indexOf(line)} ${line.subLines.indexOf(subLine)} ${subLine.rawString}';
        });
      }
    }
  } else {
    setState(() {
      videoSubtitle = null;
    });
  }
}
if (subtitles[0].parsedLines[0].subLines[1].htmlCode.b == true) {
  print('true');
}

}

那么如何将字幕与视频播放器位置同步?

现在我正在解析 srt 字符串。我如何从 srt 文件中解析它?

目前我正在使用subtitle_wrapper_package

在视频播放器中支持 vtt 字幕。

SubTitleWrapper(
  videoPlayerController: chewieController.videoPlayerController,
  subtitleController: SubtitleController(
    subtitleUrl: subtitleUrl,
    showSubtitles: true,
  ),
  subtitleStyle:
      SubtitleStyle(textColor: Colors.white, hasBorder: true),
  videoChild: Chewie(
    controller: chewieController,
  )),

输出: