-1

I need help making a CMTime variable in swift 2 so I can create a AVMutableCompostion. Current code:

    let videoAsset: AnyObject! = AVAsset(URL: outputFileURL)

    let videoDuration:CMTime = CMTimeMake(Int64(videoAsset.duration), 1)

but the variable videoDuration above keeps returning nil! I double checked to make sure the videoAsset.duration was not nil and it is not.

Abstract: leads it to crash here at the line where "videoDuration" is passed in as a peramiter

    // Merge audio and video tracks to complete video
    let videoTrack = mixComposition.addMutableTrackWithMediaType(AVMediaTypeVideo, preferredTrackID: Int32(kCMPersistentTrackID_Invalid))
    do {
        try videoTrack.insertTimeRange(CMTimeRangeMake(kCMTimeZero, videoDuration),
            ofTrack: videoAsset.tracksWithMediaType(AVMediaTypeVideo)[0] ,
            atTime: kCMTimeZero)
    } catch _ {
        /* TODO: Finish migration: handle the expression passed to error arg: kCMTimeZero */
    }

Please help! Any suggestions would be greatly appreciated :)

Garret Kaye
  • 2,412
  • 4
  • 21
  • 45

1 Answers1

0

One problem is that you are casting videoAsset to an AnyObject. Don't do that. It wants to be an AVAsset. Let it be one.

Another problem is that you are not obeying the rules for how to get an AVAsset's duration. You cannot simply create an AVAsset and immediately ask for its duration. This property (like many others) requires time to acquire. Please read the documentation on AVAsynchronousKeyValueLoading.

matt
  • 515,959
  • 87
  • 875
  • 1,141