支持srt / vtt字幕并在视频播放器抖动中同步

时间:2019-10-31 15:49:37

标签: flutter subtitle video-player srt vtt

我想在我的应用中播放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文件中解析它?

1 个答案:

答案 0 :(得分:0)

目前我正在使用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,
  )),

输出:

enter image description here