I have divided screens into multiple zones and in of them each trying to play multiple videos. The problem is the app crashes after playing videos for few seconds.
Here is the playing logic. I think the issue lies here somewhere.
@override
void initState() {
super.initState();
_initController();
}
void _initController() {
_controller = VideoPlayerController.file(File(mediaItems[currentIndex].filePath ??""))
..initialize().then((_) {
setState(() {});
_controller.setVolume(0.0);
_controller.play();
setState(() {});
});
_controller.addListener(() {
if (_controller.value.isCompleted ?? false) {
if(toogle) { //isCompleted is being called at the start of a video too, so to skipit.
currentIndex = (currentIndex + 1) % mediaItems.length;
_onControllerChange(mediaItems[currentIndex].filePath ?? "");
}
toogle = !toogle;
}
});
}
Future<void> _onControllerChange(String link) async {
if (_controller == null) {
// If there was no controller, just create a new one
_initController();
} else {
// If there was a controller, we need to dispose of the old one first
// await _controller.pause();
final oldController = _controller;
// Registering a callback for the end of next frame
// to dispose of an old controller
// (which won't be used anymore after calling setState)
WidgetsBinding.instance.addPostFrameCallback((_) async {
oldController.removeListener(() { });
oldController.dispose();
// Initing new controller
});
_initController();
// Making sure that controller is not used by setting it to null
// setState(() {
// _controller = null;
// });
}
}
@override
Widget build(BuildContext context) {
return VideoPlayer(_controller);
}
I tried using different packages for video_player like video_player_win. I tried adding a custom video player having different decoders but didn't work. I tried checking performance overlay but performance is normal until suddenly Jank occurs and app crashes. Using windows task manager I check the memory usage which was not much on avg 250 mbps.