0

I'm using the youtube_player_flutter package in my Flutter app to play YouTube videos.
Everything works fine, but I’ve noticed a strange visual glitch .when I navigate back (pop) from the video player screen to the previous screen, the video briefly flashes for a milisecond before disappearing.

Here’s a simplified version of my code:

// ignore_for_file: avoid_print
import 'package:flutter/material.dart';
import 'package:youtube_player_flutter/youtube_player_flutter.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
 const MyApp({super.key});
 @override
 Widget build(BuildContext context) {
 return MaterialApp(debugShowCheckedModeBanner: false, home: Example1());
 }
}
class Example1 extends StatelessWidget {
 const Example1({super.key});
 @override
 Widget build(BuildContext context) {
 return Scaffold(
 appBar: AppBar(title: const Text("Example 1")),
 body: Center(
 child: ElevatedButton(
 onPressed: () {
 Navigator.push(
 context,
 MaterialPageRoute(
 builder: (context) => const VideoPlayerScreen(),
 ),
 );
 },
 child: const Text("Go to video screen"),
 ),
 ),
 );
 }
}
class VideoPlayerScreen extends StatefulWidget {
 const VideoPlayerScreen({super.key});
 @override
 State<VideoPlayerScreen> createState() => _VideoPlayerScreenState();
}
class _VideoPlayerScreenState extends State<VideoPlayerScreen> {
 late YoutubePlayerController _controller;
 bool _isPlayerReady = false;
 @override
 void initState() {
 super.initState();
 const videoUrl = 'https://www.youtube.com/embed/2PuFyjAs7JA';
 final videoId = YoutubePlayer.convertUrlToId(videoUrl) ?? '2PuFyjAs7JA';
 _controller = YoutubePlayerController(
 initialVideoId: videoId,
 flags: const YoutubePlayerFlags(
 autoPlay: false,
 mute: false,
 ),
 )..addListener(_listener);
 }
 void _listener() {
 if (_isPlayerReady && mounted && !_controller.value.isFullScreen) {
 setState(() {});
 }
 }
 @override
 void deactivate() {
 _controller.pause();
 super.deactivate();
 }
 @override
 void dispose() {
 _controller.dispose();
 super.dispose();
 }
 @override
 Widget build(BuildContext context) {
 return Scaffold(
 appBar: AppBar(title: const Text('YouTube Player')),
 body: YoutubePlayerBuilder(
 player: YoutubePlayer(
 controller: _controller,
 showVideoProgressIndicator: true,
 onReady: () => _isPlayerReady = true,
 ),
 builder: (context, player) => Column(children: [player]),
 ),
 );
 }
}
New contributor
Prajwal Karki is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

1 Answer 1

0

To avoid this issue pause the video before navigating here is the codo to do it:

return WillPopScope(
 onWillPop: () async {
 //This code will run before navigating back
 _controller.pause();
 return true;
 },
 child: Scaffold(
 appBar: AppBar(
 title: const Text('YouTube Player'),
answered 2 days ago
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.