12

screenshot

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.

2
  • 1
    How did you solve? Could you share your implementation of page view builder with Chewie Video? Thanks! Commented Jul 30, 2020 at 16:33
  • 1
    Please someone give the answer I am also getting same issue Commented Nov 2, 2020 at 9:36

4 Answers 4

4

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;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Well done Adrian, yes we should remove the widget or change it if we are going to dispose the player.
I was missing the _controller=null piece; worked after that for me.
_controller.dispose(); does not clear the instance in the memory and then set it to null automatically?
@Manas back when i posted this, it was a hit or miss. I guess it was a race condition.
2

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.

Comments

0

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();
}

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0

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();
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.