3

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.

João Abrantes
  • 4,772
  • 4
  • 35
  • 71

1 Answers1

6

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();
  }
}
Amir_P
  • 8,322
  • 5
  • 43
  • 92
  • This is not working for me. The position `videoPlayerController.value.position` is only being updated every 500 ms so that codes prints 5 times the same position and then prints a different one 500 ms ahead – João Abrantes Sep 18 '20 at 17:38
  • 1
    My bad! I've updated the answer check it again. @JoãoAbrantes – Amir_P Sep 18 '20 at 19:35