salvadordeveloper / flutter-tiktok

A TikTok Clone in Flutter and Firebase.
MIT License
1.08k stars 396 forks source link

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

Open luistrivelatto opened 2 years ago

luistrivelatto commented 2 years ago

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.

yilmazedis commented 3 months ago

Hi, did you fix the issue