7

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?

Mike Akers
  • 12,039
  • 14
  • 58
  • 71

1 Answers1

12

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
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • 2
    The problem was I wasn't importing CoreMedia. For some reason I thought the CMTime constructor for NSValue was defined in AVFoundation. Thanks! – Mike Akers Oct 17 '14 at 17:52
  • 3
    For swift 3 have a look here: http://stackoverflow.com/questions/38669061/nsvaluecmtime-in-swift-3 – David Schmoecker Oct 22 '16 at 12:12