Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: StateError (Bad state: Cannot add new events after calling close) #92

Open
SX-Code opened this issue Aug 17, 2024 · 0 comments
Open
Labels
bug Something isn't working

Comments

@SX-Code
Copy link

SX-Code commented Aug 17, 2024

Description
A clear and concise description of what the bug is.

Steps To Reproduce

  1. First screen:
// controller
VideoPlayerController _controller = VideoPlayerController.networkUrl(
  Uri.parse(url),
  videoPlayerOptions: VideoPlayerOptions(
    allowBackgroundPlayback: true,
  ),
);

SubtitleController _subtitleController = SubtitleController(
  subtitleUrl: subtitle,
  subtitleType: SubtitleType.srt,
);

// use
SubtitleWrapper(
  videoPlayerController: _controller!,
  subtitleController: _subtitleController!,
  subtitleStyle: const SubtitleStyle(
    textColor: Colors.white,
    hasBorder: true,
  ),
  videoChild: AspectRatio(
    aspectRatio: _controller!.value.aspectRatio,
    child: VideoPlayer(_controller!),
  ),
)

  1. Navigate to a second screen:
Navigator.push(
  context,
  MaterialPageRoute(
    builder: (_) => VideoPlayerFullScreen(
      controller: videoController,
      subtitleController: subtitleController,
    ),
  ),
);
  1. Second screen:
SubtitleWrapper(
  videoPlayerController: widget.controller,
  subtitleController: widget.subtitleController!,
  subtitleStyle: const SubtitleStyle(
    textColor: Colors.white,
    hasBorder: true,
  ),
  videoChild: AspectRatio(
    aspectRatio: widget.controller.value.aspectRatio,
    child: VideoPlayer(widget.controller),
  ),
)
  1. See error
StateError (Bad state: Cannot add new events after calling close)

Solution
Remove the listener in a timely manner:
lib/bloc/subtitle/subtitle_bloc.dart

void _listener() {
  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,
      ),
    );
  }
}

Future<void> loadSubtitle({
  required Emitter<SubtitleState> emit,
}) async {
  emit(LoadingSubtitle());
  videoPlayerController.addListener(_listener);
}

@override
Future<void> close() {
  subtitleController.detach();
  // remove listener
  videoPlayerController.removeListener(_listener);
  return super.close();
}
@SX-Code SX-Code added the bug Something isn't working label Aug 17, 2024
@SX-Code SX-Code changed the title fix: Remove the listener in a timely manner fix: StateError (Bad state: Cannot add new events after calling close) Aug 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant