salvadordeveloper / flutter-tiktok

A TikTok Clone in Flutter and Firebase.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

First video freezes due to race condition with loading of first videos

luistrivelatto opened this issue · comments

I noticed that sometimes when running the app, the first video froze after a few frames and the audio from the 2nd video started playing. After analyzing, it seems to be due to a race condition on initState of _FeedScreenState:

  void initState() {
    feedViewModel.loadVideo(0);
    feedViewModel.loadVideo(1);

    super.initState();
  }
  void loadVideo(int index) async {
    if (videoSource!.listVideos.length > index) {
      await videoSource!.listVideos[index].loadController();
      videoSource!.listVideos[index].controller?.play();
      notifyListeners();
    }
  }

Both videos are loaded asynchronously, and after loading the video starts to get played. If the 1st video gets loaded before the 2nd, then when the 2nd finishes loading we call controller.play() which starts playing the audio from the 2nd video.