Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert a CMTime to an NSValue in Swift?

I was trying to make use of AVPlayer's addBoundaryTimeObserverForTimes method from swift and ran into a problem converting CMTimes to NSValues, which is what addBoundaryTimeObserverForTimes take as input. In the end I resorted to wrapping [NSString valueWithCMTime:] in a C function and calling it from swift, but is there a way to do it in pure swift?

like image 499
Mike Akers Avatar asked Oct 17 '14 17:10

Mike Akers


1 Answers

The Swift constructor corresponding to valueWithCMTime: is NSValue(time:):

import CoreMedia
import AVFoundation

let cmTime  = CMTimeMake(value: 10, timescale: 20) // Updated for Swift 3+
let cmValue = NSValue(time: cmTime)
// Or simply:
let cmValue = cmTime as NSValue
like image 185
Martin R Avatar answered Oct 15 '22 00:10

Martin R