youtube_player_flutter icon indicating copy to clipboard operation
youtube_player_flutter copied to clipboard

[BUG] Video player not disappeared immediately when pop out of screen

Open letanssang opened this issue 1 year ago • 6 comments

Describe the bug When pop out of screen contains youtube video player, the video player didn't disappeared immediately and there is an exception. (i have disposed youtube controller in dispose method) A YoutubePlayerController was used after being disposed. Once you have called dispose() on a YoutubePlayerController, it can no longer be used.

Expected behavior video player is disappeared before screen is disposed.

Screenshots

https://github.com/sarbagyastha/youtube_player_flutter/assets/67082439/fc6badd9-7d92-4a21-88ac-1f8da8b9bea9

Here is my code

import 'package:youtube_player_flutter/youtube_player_flutter.dart';

class PhoneticView extends ConsumerStatefulWidget {
  const PhoneticView({super.key});

  @override
  ConsumerState<PhoneticView> createState() => _PhoneticViewState();
}

class _PhoneticViewState extends ConsumerState<PhoneticView> {
  YoutubePlayerController? _youtubePlayerController;
  Phonetic phonetic = Phonetic.initial();

  @override
  void initState() {
    super.initState();
    Future.delayed(
      Duration.zero,
      () => _init(),
    );
  }

  Future<void> _init() async {
    phonetic = ModalRoute.of(context)!.settings.arguments as Phonetic;
    _youtubePlayerController = YoutubePlayerController(
      initialVideoId: phonetic.youtubeVideoId,
      flags: const YoutubePlayerFlags(
          hideThumbnail: true,
          autoPlay: true,
          captionLanguage: 'vi',
          enableCaption: true,
          loop: true,
          startAt: 3),
    );
    setState(() {});
  }

  @override
  void deactivate() {
    _youtubePlayerController?.pause();
    super.deactivate();
  }

  @override
  void dispose() {
    _youtubePlayerController?.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: const AppBackButton(),
        title: Text(phonetic.phonetic),
      ),
      body: _youtubePlayerController == null
          ? const Center(
              child: AppLoadingIndicator(),
            )
          : Padding(
              padding: const EdgeInsets.all(8.0),
              child: Column(
                children: [
                  YoutubePlayer(
                      controller: _youtubePlayerController!,
                      showVideoProgressIndicator: true,
                      progressIndicatorColor: Theme.of(context).primaryColor,
                      progressColors: ProgressBarColors(
                        playedColor: Theme.of(context).primaryColor,
                        handleColor: Theme.of(context).primaryColor,
                      ),
                      bottomActions: [
                        const SizedBox(width: 14.0),
                        CurrentPosition(),
                        const SizedBox(width: 8.0),
                        ProgressBar(
                            isExpanded: true,
                            controller: _youtubePlayerController,
                            colors: ProgressBarColors(
                              playedColor: Theme.of(context).primaryColor,
                              handleColor: Theme.of(context).primaryColor,
                            )),
                        RemainingDuration(),
                      ]),
                  ...
                ],
              ),
            ),
    );
  }
}

letanssang avatar Sep 19 '23 02:09 letanssang

it works okay with package youtube_player_iframe

letanssang avatar Sep 19 '23 15:09 letanssang

I'm also experiencing this bug

DavidOrakpo avatar Oct 24 '23 15:10 DavidOrakpo

i have the same problem

SpagnolRafael avatar Oct 30 '23 20:10 SpagnolRafael

To get around this, I basically used set state to replace the YouTube player widget with a sinzedbox, just before popping the page

DavidOrakpo avatar Oct 30 '23 23:10 DavidOrakpo

I solved this issue by setting useHybridComposition to false in YoutubePlayerFlags

Jason4Ever avatar Jan 06 '24 06:01 Jason4Ever

set useHybridComposition to false is not working for me. I send a PR to fix

chrisynchen avatar Apr 15 '24 15:04 chrisynchen