Tuesday, September 11, 2018

Convert NSDate from AVDateRangeMetadataGroup to CMTime

Leave a Comment

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).

1 Answers

Answers 1

I hope it is the easiest solution: You can try using CMTime.init(seconds: Double, preferredTimescale: CMTimeScale) You can calculate the seconds from your NSDate objects and that way pass them into CMTime's function. This might sound trivial but hope it helps.

Also as I mentioned above to know when currentTime is not in the nil state try overriding the property and set a property observer on it and put a print statement into your override. That way you will see in the debug console whenever it will change state. You can then trace it and set brakepoints to it.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment