youtube_player_flutter
youtube_player_flutter copied to clipboard
[BUG] Video player not disappeared immediately when pop out of screen
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(),
]),
...
],
),
),
);
}
}
it works okay with package youtube_player_iframe
I'm also experiencing this bug
i have the same problem
To get around this, I basically used set state to replace the YouTube player widget with a sinzedbox, just before popping the page
I solved this issue by setting useHybridComposition to false in YoutubePlayerFlags
set useHybridComposition
to false
is not working for me. I send a PR to fix