AV Foundation reporting player item duration incorrectly

491 views Asked by At

I am trying to play a video on iOS using AVPlayer that is encoded with zencoder. The problem I am seeing is that the duration that the player item reports is rounded / imprecise. For example, the video duration might be 173.134 and the player item will report it as a flat 174.0. This causes problems with detecting loaded percentage and other related things. If I try to use the video without encoding everything is reported correctly and precisely.

Has anyone else ever seen this or have a solution?

2

There are 2 answers

0
drewag On BEST ANSWER

The problem turned out to be a problem with the source video / zencoder. The audio track was a slightly different length than the video which caused problems with the encoding. Cutting off the last second of the video so that the track durations would match up fixed the problem.

1
Valentin Shamardin On

I had the same problem. I just compare difference between current position and item duration and 1 second:

- (void)playing:(CMTime)time
{
   CMTime itemDuration = _player.currentItem.asset.duration;
   NSTimeInterval currentTime = CMTimeGetSeconds(time);
   NSTimeInterval duration = CMTimeGetSeconds(itemDuration);
   if (fabs(currentTime - duration) < 1)
      // This is the end.
}