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

video player in v3 triggers ANR after first hot reload #43

Open
VerioN1 opened this issue Feb 7, 2025 · 2 comments
Open

video player in v3 triggers ANR after first hot reload #43

VerioN1 opened this issue Feb 7, 2025 · 2 comments

Comments

@VerioN1
Copy link

VerioN1 commented Feb 7, 2025

I tested version 3 on iOS and I saw that when trying to hot reload my app, the app gets into ANR and I need to fully reset it, something that didn't happen in v2. for now going back to v2 until fixed :)
using Flutter 3.24.4
video source : file / network

code to replicate:


class VideoListItem extends StatefulWidget {
  const VideoListItem({
    required this.videoSource,
    super.key,
  });
  final VideoSource videoSource;

  @override
  State<VideoListItem> createState() => _VideoListItemViewState();
}

class _VideoListItemViewState extends State<VideoListItem> {
  NativeVideoPlayerController? _controller;
  StreamSubscription<void>? _eventsSubscription;

  bool _isPlaying = true;

  @override
  void dispose() {
    _eventsSubscription?.cancel();
    _controller?.dispose();
    _controller = null;
    super.dispose();
  }

  @override
  void didUpdateWidget(VideoListItem oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (oldWidget.videoSource != widget.videoSource && context.mounted) {
      unawaited(_controller?.loadVideo(widget.videoSource));
    }
  }

  void _onPlaybackStatusChanged() {
    setState(() {});
  }

  void _onPlaybackPositionChanged() {
    // setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        print('tap tap atp');
        togglePlayback();
      },
      child: AbsorbPointer(
        child: AspectRatio(
          aspectRatio: 16 / 9,
          child: VisibilityDetector(
            key: Key(widget.videoSource.path),
            onVisibilityChanged: (info) async {
              if (!context.mounted) return;
              if (info.visibleFraction < 0.15) {
                await _controller?.pause();
              } else {
                await _controller?.play();
              }
            },
            child: NativeVideoPlayerView(
              onViewReady: (controller) async {
                _controller = controller;
                await _controller?.setVolume(1);
                await _controller?.loadVideo(widget.videoSource);
                _eventsSubscription = _controller?.events.listen((event) {
                  switch (event) {
                    case PlaybackStatusChangedEvent():
                      _onPlaybackStatusChanged();
                    case PlaybackPositionChangedEvent():
                      _onPlaybackPositionChanged();
                    case PlaybackEndedEvent():
                      _onPlaybackEnded();
                    case PlaybackSpeedChangedEvent():
                    // TODO: Handle this case.
                    case VolumeChangedEvent():
                    // TODO: Handle this case.
                    case PlaybackReadyEvent():
                    // TODO: Handle this case.
                    case PlaybackErrorEvent():
                    // TODO: Handle this case.
                  }
                });
              },
            ),
          ),
        ),
      ),
    );
  }

  Future<void> togglePlayback() async {
    final controller = _controller;
    if (controller == null) return;

    if (_isPlaying) {
      await controller.pause();
    } else {
      await controller.play();
    }
    final isPlaying = await controller.isPlaying();
    setState(() {
      _isPlaying = isPlaying;
    });
  }

  void _onPlaybackEnded() {
    final controller = _controller;
    if (controller == null) return;

    unawaited(controller.stop());
    unawaited(controller.play());
  }
}

Copy link

github-actions bot commented Feb 7, 2025

Thank you for opening this issue!

I will look into it as soon as possible. In the meantime, please review these important project updates:

Your feedback and contributions are greatly appreciated!

@VerioN1 VerioN1 changed the title video player in v3 in ios ANR after first hot reload video player in v3 ANR after first hot reload Feb 7, 2025
@VerioN1 VerioN1 changed the title video player in v3 ANR after first hot reload video player in v3 triggers ANR after first hot reload Feb 7, 2025
@albemala
Copy link
Owner

@VerioN1 you don't need a VisibilityDetector, NativeVideoPlayerViews are automatically stopped and disposed when going off screen. That might be why you get ANR issues. I've tested your code without the visibility detector and hot-reloading the app works fine for me. Let me know if you still have issues, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants