How to get full duration of HLS video in flutter video_player on Android?

Issue

I’m trying to play HLS video in flutter video_player. I’m generating m3u8 file like this:

#EXT-X-VERSION:4
#EXT-X-TARGETDURATION:10
#EXT-X-MEDIA-SEQUENCE:1609034710
#EXT-X-PLAYLIST-TYPE:EVENT
#EXTINF:10.0,
video_part1.ts
#EXTINF:10.0,
video_part2.ts
#EXTINF:10.0,
video_part3.ts
#EXTINF:10.0,
video_part4.ts
#EXTINF:10.0,
video_part5.ts

... // etc.

I do not change or delete old parts, only append new ones when they available.

Video plays normally, but when I check controller.value.duration it always shows 50 seconds – the duration of the first playlist before any updates, and it does not allow me to seekTo beyond that position.

I can count real duration from playlist, but question is: How can I put that duration into a player and seek to any position in it?

Solution

After some research:

  • All players, using video_player underneath, have the same behavior and do not update playback duration correctly (Chewie, YoYo Player, Better player etc.)
  • In order to change this behavior you probably need to fork default video_player implementation and add (or change) some code inside of it. There are some events inside which update different parameters of controller.value, so you probably just need update duration somewhere there. I decided to give it a try later then, but not now.
  • Extending default VideoPlayerController does not help, as it contains lots of private variables which can not be accessed in inherited code.
  • There is a newer 2.0.0 version of video_player which probably solves this problem (at least it solves bug when controller.value.isBuffering is always false), but at the moment it’s in beta and requires beta flutter and dart sdk with the null safety feature. It won’t be a problem in the future when all the code migrates to null safety, but now it requires to update lots of my code in the project, and does not guarantie that the problem will be solved after that, so I did not use that.

Ended up using flutter_playout for this task. It’s NOT based on default video_player and does not have this bug (but probably has others, need to use it more), and it works!

Answered By – Som-1

Answer Checked By – Dawn Plyler (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.