
I am using chewie player to play a list of videos in pageview.builder. when i scroll my page below it works fine,but when i scroll my page to view top video it shows me this error.How can i re-initialize the videoplayercontroller.

I am using chewie player to play a list of videos in pageview.builder. when i scroll my page below it works fine,but when i scroll my page to view top video it shows me this error.How can i re-initialize the videoplayercontroller.
problem is you have to destroy the container that is using the video player and setting .dipose() after its destroyed. So your kill function should be something like this:
void killVidPlayer() {
yourContainer = Container();
setState(() {
});
_controller.dispose();
_controller = null;
}
So i followed what this guy said here and it worked github
void initState() {
super.initState(); //Super should be called at the very beginning of init
_controller = VideoPlayerController.network(
'http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerFun.mp4',
);
_chewieController = ChewieController(
videoPlayerController: _controller,
aspectRatio: 3 / 2,
autoPlay: true,
looping: true,
);
}
@override
void dispose() {
_controller.dispose();
_chewieController.dispose();
super.dispose(); //Super should be called at the very end of dispose
}
//Let me know if this doesn't solve your issue.
It's working for me.
@override
void dispose() {
// It is necessary to pause before dispose, otherwise an error will occur during dispose
// [Once you have called dispose() on a YoutubePlayerController, it can no longer be used.]
_youtubeController?.pause();
// After playback stops, dispose
Future.delayed(const Duration(milliseconds: 40)).then((value) => _youtubeController?.dispose());
super.dispose();
}
I found the solution. This error occurs when I exit the page and the dispose method is called automatically before the initState method completes. This happens because, during the initState call, the code waits to fetch and display the video, causing a slight delay in loading part of the video and initializing the controller.
At this moment, if the user exits the page before the controller is fully initialized, the dispose method is called, disposing of the controller before it is initialized, which causes the issue. To avoid this, you can add a variable to check the initialization state as follows:
void onInit() async { // this Like initState just I use GetX package
isLoading.value = true;
course = await getCourseDetails.getMyCourseList(16, courseId ?? 10); // line just for my code, ignore it, and here my code waits for the data to get
if (!isClose) { // here I check if the screen is closed, don't init
initVideoController();
isInit = true;
}
}
@override
void onClose() { // this Like dispose just I use GetX package
if (isInit) { // if I initialized the controller, then dispose of it
betterPlayerController.pause();
betterPlayerController.dispose();
_playController.close();
}
isClose = true; // this is for if I close the screen before completing the init method to not initialize the controller
super.onClose();
}