sarbagyastha / youtube_player_flutter

A Flutter plugin for inline playback or streaming of YouTube videos using the official iFrame Player API.
https://youtube.sarbagyastha.com.np
BSD 3-Clause "New" or "Revised" License
690 stars 766 forks source link

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

Closed letanssang closed 1 week ago

letanssang commented 11 months ago

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 commented 11 months ago

it works okay with package youtube_player_iframe

DavidOrakpo commented 9 months ago

I'm also experiencing this bug

SpagnolRafael commented 9 months ago

i have the same problem

DavidOrakpo commented 9 months ago

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

Jason4Ever commented 7 months ago

I solved this issue by setting useHybridComposition to false in YoutubePlayerFlags

chrisynchen commented 4 months ago

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