swift and CMTimeMake

Andrew Skrypnik picture Andrew Skrypnik · Jun 25, 2014 · Viewed 16.2k times · Source

I try to capture video:
https://developer.apple.com/library/ios/documentation/AudioVideo/Conceptual/AVFoundationPG/Articles/04_MediaCapture.html#//apple_ref/doc/uid/TP40010188-CH5-SW26

var maxDuration : CMTime = CMTimeMake(seconds, preferredTimeScale)
aMovieFileOutput.maxRecordedDuration = CMTimeMake(seconds, preferredTimeScale)

1 row have error: Use of module 'CMTime' as a type
2 row have error: Use of unresolved identifier 'CMTimeMake'

What I do wrong?

Answer

Martin R picture Martin R · Jun 25, 2014

CMTime and CMTimeMake are defined in the "CoreMedia" module, therefore you have to

import CoreMedia

Then this compiles without problems:

let seconds : Int64 = 10
let preferredTimeScale : Int32 = 1
let aMovieFileOutput = AVCaptureMovieFileOutput()
let maxDuration : CMTime = CMTimeMake(seconds, preferredTimeScale)
aMovieFileOutput.maxRecordedDuration = maxDuration

Update for Swift 3:

let maxDuration = CMTime(seconds: Double(seconds), preferredTimescale: 1)