support srt/vtt subtitle and syncing in video player flutter

浪子不回头ぞ 提交于 2019-12-08 09:50:46

问题


I want to play srt subtitles in my app. I have integrated srt_parser so far. Also, I am able to get the current position of the player by this code

  inMilliseconds = _videoPlayerController.value.position.inMilliseconds;

Here is srt parse code.

  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');
}

}

So How can i sync subtitle with Video player position?

And right now I am parsing srt string. How can I parse it from srt file?


回答1:


For now I am using subtitle_wrapper_package

which supports vtt subtitle in the video player.

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

Output:



来源:https://stackoverflow.com/questions/58646998/support-srt-vtt-subtitle-and-syncing-in-video-player-flutter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!