ios - 确定 SceneKit 中 SKVideoNode 的视频大小/纵横比
<p><p>如何从 AVPlayer 获取视频的视频大小来设置节点的几何大小?</p>
<p>例如,我有一个宽度和高度的 SCNPlane</p>
<pre><code>let planeGeo = SCNPlane(width: 5, height: 5)
</code></pre>
<p>所以现在我实例化我的视频播放器</p>
<pre><code>let videoURL = NSURL(string: someURL)
let player = AVPlayer(URL: videoURL!)
</code></pre>
<p>还有我的 SKVideoNode</p>
<pre><code>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()
</code></pre>
<p>所以现在我想要视频大小以将我的飞机调整为正确的纵横比。我已经摆弄了 AVLPlayerLayer 但这给了我总是 0</p>
<pre><code>let avLayer = AVPlayerLayer(player: player)
print(avLayer.videoRect.width) //0
print(avLayer.videoRect.height) //0
</code></pre>
<p>我在这里也试过了,但效果不佳</p>
<pre><code>let avLayer = AVPlayerLayer(player: player)
let layer = avLayer.sublayers!
let transformedBounds = CGRectApplyAffineTransform(layer.bounds, CATransform3DGetAffineTransform(layer.sublayerTransform))
print(transformedBounds.width) //0
print(transformedBounds.height) //0
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>好的,我想通了,KVO 是要走的路。添加viewDidLoad:</p>
<pre><code>player.currentItem?.addObserver(self, forKeyPath: "presentationSize", options: .New, context: nil)
</code></pre>
<p>在 deinit 中:</p>
<pre><code>player.currentItem?.removeObserver(self, forKeyPath: "presentationSize")
</code></pre>
<p>然后添加:</p>
<pre><code>override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: ?, 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
}
}
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 确定 SceneKit 中 SKVideoNode 的视频大小/纵横比,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/34928396/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/34928396/
</a>
</p>
页:
[1]