24

AVPlayer primarily measures times with CMTime. However when using an AVPlayerItemMetadataCollector to collect date range metadata from an HLS stream, the resulting AVDateRangeMetadataGroups hold NSDates instead of CMTimes.

My problem is I want to add a boundary time observer to the player so I know when the playback head actually enters the date range, but you can only add a boundary time observer using CMTime. AVPlayer and AVPlayerItem can seek to a Date and can give me the current playback head as a Date, but I don't see any way to convert a given Date into a CMTime that I can use to set a boundary time observer.

In theory if I knew what the EXT-X-PROGRAM-DATE-TIME tag said I could calculate a CMTime offset myself, but I don't see a way to retrieve that.

The only other thought I had was to retrieve both currentTime and currentDate from the AVPlayerItem, then calculate the relative offset between currentDate and my target date and apply that to the CMTime. But a problem with this solution is the currentDate and currentTime won't represent the exact same time. The delta is presumably going to be very small, but it won't be zero (unless the rate itself is zero).

Lily Ballard
  • 182,031
  • 33
  • 381
  • 347
  • I would also accept any recommendation for how to know when `currentDate` becomes non-`nil`, because if I can read that as soon as it becomes available initially, that will presumably be before I'm actually playing the stream and therefore the effective rate will be zero. – Lily Ballard Aug 31 '18 at 21:54
  • It's also worth noting that if the stream includes any discontinuous media segments, trying to calculate a time using `currentDate` and `currentTime` won't be reliable (as that won't know about the discontinuity). – Lily Ballard Aug 31 '18 at 23:05
  • Do you have any code to show or any minimum working project examples to try? How about setting a property observer on a variable which would trigger a print statement once that is being set using a get and set ?? – AD Progress Sep 11 '18 at 11:41
  • `currentDate` has no setter, so there's nothing to override. It's a computed property, based on both `currentTime` and on internal data that isn't exposed to the user (namely, a time<->date mapping based on `EXT-X-PROGRAM-DATE-TIME` tags from the media playlists with no API to query it). – Lily Ballard Sep 11 '18 at 18:19
  • I've hit the same issue. I was going down the route of matching `currentTime` and `currentDate`. I'm curious how you know that the `currentTime` and `currentDate` do not represent the same time? I am seeing this from experimentation, but I didn't expect this. Also, did you ever manage to find a solution? (P.S. Your issue SR-6795 on Swift KVO and swizzling saved me potentially days of headache tracking the cause of a random crash, so thank you for that). – theRealRobG Mar 23 '20 at 10:02

0 Answers0