简体   繁体   English

如何在 Swift 中将 CMTime 转换为 NSValue?

[英]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.我试图从 swift 中使用 AVPlayer 的addBoundaryTimeObserverForTimes方法,但在将CMTimes转换为NSValues遇到了问题,这就是addBoundaryTimeObserverForTimes作为输入的内容。 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?最后我求助于将 [NSString valueWithCMTime:] 包装在一个 C 函数中并从 swift 调用它,但是有没有办法在纯 swift 中做到这一点?

The Swift constructor corresponding to valueWithCMTime: is NSValue(time:) : valueWithCMTime:对应的 Swift 构造函数是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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM