appinioGmbH / flutter_packages

Dart and Flutter plugins/packages used and maintained by @appinioGmbH
193 stars 224 forks source link

Network video url initialize takes time #83

Closed RzTutul closed 1 year ago

RzTutul commented 1 year ago

I am trying to build video reels like Instagram or TikTok. I used appinio_video_player 1.2.0 but when I swap a new video then it takes time to initialize. I initialized the controller at initState. can you resolve this?

khanmujeeb687 commented 1 year ago

Hey, @RzTutul are you using PageView?

RzTutul commented 1 year ago

@khanmujeeb687 Yes i used PageView

khanmujeeb687 commented 1 year ago

Okay, So what happens is when you swipe to a page in pageview it builds at that moment, and initState gets called at that moment that's why it takes time to initialize the video. One top-rated solution for this issue is to load the next page before when we are on the previous page. Flutter Pageview I think does not support this functionality. So we can achieve it using this plugin: https://pub.dev/packages/preload_page_view. This is the same as pageview but you can pass the count of pages you want to preload.

RzTutul commented 1 year ago

@khanmujeeb687 I tried PreLoadPageView but It is playing all the videos at the same time. Also, it is not disposing of the video controller of the previous page while scrolling to the next page. Could you please help me out with this?

@override Widget build(BuildContext context) { return new PreloadPageView.builder( itemCount: ..., itemBuilder: ..., onPageChanged: (int position) {...}, ..... preloadPagesCount: 3, controller: PreloadPageController(), ); }

khanmujeeb687 commented 1 year ago

Hey @RzTutul , Since you have set the preloadPagesCount to 3 so it will keep the current , previous and next page loaded. If you want only the current video to play then maybe you can set the videos to pause by default or maybe you can find some other workaround for that.

khanmujeeb687 commented 1 year ago

If you are familiar with provider and already using it in your project for state management. Then you can store the currentVideoId in a variable and add a listener to it, so when it changes so depending upon the ID you can play and pause the video.