问题
How can I get the video size of a video from AVPlayer to set the geometry size of my node?
For example, I have an SCNPlane with a width and height
let planeGeo = SCNPlane(width: 5, height: 5)
So now I instantiate my video player
let videoURL = NSURL(string: someURL)
let player = AVPlayer(URL: videoURL!)
and my 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()
So now I want to have the video size to resize my plane to a correct aspect ratio. I already fiddled around with AVLPlayerLayer but this gives me always 0
let avLayer = AVPlayerLayer(player: player)
print(avLayer.videoRect.width) //0
print(avLayer.videoRect.height) //0
Also I tried that here but it doesn't work as well
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
回答1:
Ok I figured it out, KVO is the way to go. Add in viewDidLoad:
player.currentItem?.addObserver(self, forKeyPath: "presentationSize", options: .New, context: nil)
in deinit:
player.currentItem?.removeObserver(self, forKeyPath: "presentationSize")
and then add:
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
}
}
}
来源:https://stackoverflow.com/questions/34928396/determine-size-aspect-ratio-of-video-for-skvideonode-in-scenekit