Lets assume I have a CMTimeRange
constructed from start time
zero, and
duration
of 40 seconds.
I want to split this CMTimeRange
into multiple chunks by a X seconds divider. So the total duration
of the chunks will be the same duration
as the original duration, and each startTime
will reflect the endTime
of of the previous chunk. The last chunk will be the modulus of the left over seconds.
For example, for video of 40 seconds, and divider of 15 seconds per chunk:
- First
CMTimeRange
- start time: 0, duration: 15 seconds. - Second
CMTimeRange
- start time: 15, duration: 15 seconds. - Third
CMTimeRange
- start time: 30, duration: 10 seconds.(left overs)
What I've tried:
I tried using CMTimeSubtract
on the total duration and use the result again, in recursive way untill the CMTime in invalid, But it doesn't seems to work.
Any help will be highly appreciated.
Best Regards, Roi
I too needed to stride
CMTime
, to deal withAVCaptureDevice
exposure durations & showing these to users.Turns out Martin's answer doesn't work anymore with the changes in Swift 4.x / XCode 10. Here's my version of
CMTime
conformance toStrideable
:I derped with it in a playground and it seems to work.
Starting at
range.start
, create ranges of the given length untilrange.end
is reached:intersection
is used here to prune the last chunk to the original range.Alternative solution:
With a custom extension to make
CMTime
adopt theStrideable
protocolthis can be further simplified to
In any case, you'll might want to add a check
to your function, in order to detect invalid calls during development.