loadSubtitle method
Implementation
Future<void> loadSubtitle({
required Emitter<SubtitleState> emit,
}) async {
emit(LoadingSubtitle());
videoPlayerController.addListener(
() {
final videoPlayerPosition = videoPlayerController.value.position;
if (subtitles.subtitles.isNotEmpty &&
videoPlayerPosition.inMilliseconds >
subtitles.subtitles.last.endTime.inMilliseconds) {
add(CompletedShowingSubtitles());
}
for (final subtitleItem in subtitles.subtitles) {
final validStartTime = videoPlayerPosition.inMilliseconds >
subtitleItem.startTime.inMilliseconds;
final validEndTime = videoPlayerPosition.inMilliseconds <
subtitleItem.endTime.inMilliseconds;
final subtitle = validStartTime && validEndTime ? subtitleItem : null;
if (validStartTime && validEndTime && subtitle != _currentSubtitle) {
_currentSubtitle = subtitle;
} else if (!_currentSubtitleIsValid(
videoPlayerPosition: videoPlayerPosition.inMilliseconds,
)) {
_currentSubtitle = null;
}
add(
UpdateLoadedSubtitle(
subtitle: _currentSubtitle,
),
);
}
},
);
}