flutterflutter-video-player

Flutter - video_player listener being called very slowly


I want to get the current position of the video being played in real-time. I was thinking of using a listener, but if I do:

_controller.addListener(() => print(_controller.value.position.inMilliseconds))

It only prints the value every 500 milliseconds. This is too slow, the video is updating every 33ms or even more often. Anyone knows why is this happening and what's the proper way of achieving what I want?

P.S. I was able to achieve what I wanted by starting an AnimationController when the video starts, but this seems like a hack.


Solution

  • The reason for the delay is that VideoPlayerController is notifying listeners every 500 milliseconds. You can use a Timer to periodically get position of video player. Here is a sample code to do that

    class VideoPlayerScreen extends StatefulWidget {
      @override
      VideoPlayerState createState() => VideoPlayerState();
    }
    
    class VideoPlayerState extends State<VideoPlayerScreen> {
      Timer _timer;
      VideoPlayerController videoPlayerController;
    
      void startTimer() {
        _timer = Timer.periodic(const Duration(milliseconds: 100), (Timer timer) async {
          print(await videoPlayerController.position);
        });
      }
      
      @override
      void dispose() {
        _timer?.cancel();
        videoPlayerController?.dispose();
        super.dispose();
      }
    }