如何从 AVPlayer 获取视频的视频大小来设置节点的几何大小?
例如,我有一个宽度和高度的 SCNPlane
let planeGeo = SCNPlane(width: 5, height: 5)
所以现在我实例化我的视频播放器
let videoURL = NSURL(string: someURL)
let player = AVPlayer(URL: videoURL!)
还有我的 SKVideoNode
let spriteKitScene = SKScene(size: CGSize(width: 1920, height: 1080))
spriteKitScene.scaleMode = .AspectFit
videoSpriteKitNode = SKVideoNode(AVPlayer: player)
videoSpriteKitNode.anchorPoint = CGPointMake(0,0)
videoSpriteKitNode.size.width = spriteKitScene.size.width
videoSpriteKitNode.size.height = spriteKitScene.size.height
spriteKitScene.addChild(videoSpriteKitNode)
planeGeo!.firstMaterial.diffuse.contents = spriteKitScene
videoSpriteKitNode.play()
所以现在我想要视频大小以将我的飞机调整为正确的纵横比。我已经摆弄了 AVLPlayerLayer 但这给了我总是 0
let avLayer = AVPlayerLayer(player: player)
print(avLayer.videoRect.width) //0
print(avLayer.videoRect.height) //0
我在这里也试过了,但效果不佳
let avLayer = AVPlayerLayer(player: player)
let layer = avLayer.sublayers![0]
let transformedBounds = CGRectApplyAffineTransform(layer.bounds, CATransform3DGetAffineTransform(layer.sublayerTransform))
print(transformedBounds.width) //0
print(transformedBounds.height) //0
好的,我想通了,KVO 是要走的路。添加viewDidLoad:
player.currentItem?.addObserver(self, forKeyPath: "presentationSize", options: .New, context: nil)
在 deinit 中:
player.currentItem?.removeObserver(self, forKeyPath: "presentationSize")
然后添加:
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
if keyPath == "presentationSize" {
if let item = object as? AVPlayerItem {
let size = item.presentationSize
let width = size.width
let height = size.height
//Set size of geometry here
}
}
}
关于ios - 确定 SceneKit 中 SKVideoNode 的视频大小/纵横比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34928396/
欢迎光临 OGeek|极客世界-中国程序员成长平台 (https://ogeek.cn/) | Powered by Discuz! X3.4 |