Nic*_* S. 5 video avplayer scenekit skvideonode swift
如何从 AVPlayer 获取视频的视频大小来设置节点的几何大小?
例如,我有一个具有宽度和高度的 SCNPlane
let planeGeo = SCNPlane(width: 5, height: 5)
Run Code Online (Sandbox Code Playgroud)
所以现在我实例化我的视频播放器
let videoURL = NSURL(string: someURL)
let player = AVPlayer(URL: videoURL!)
Run Code Online (Sandbox Code Playgroud)
和我的 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()
Run Code Online (Sandbox Code Playgroud)
所以现在我想要视频大小来将我的飞机大小调整为正确的宽高比。我已经摆弄了 AVLPlayerLayer 但这总是给我 0
let avLayer = AVPlayerLayer(player: player)
print(avLayer.videoRect.width) //0
print(avLayer.videoRect.height) //0
Run Code Online (Sandbox Code Playgroud)
我也在这里尝试过,但效果不佳
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
Run Code Online (Sandbox Code Playgroud)
好吧,我明白了,KVO 是可行的方法。在viewDidLoad中添加:
player.currentItem?.addObserver(self, forKeyPath: "presentationSize", options: .New, context: nil)
Run Code Online (Sandbox Code Playgroud)
在 deinit 中:
player.currentItem?.removeObserver(self, forKeyPath: "presentationSize")
Run Code Online (Sandbox Code Playgroud)
然后添加:
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
}
}
}
Run Code Online (Sandbox Code Playgroud)
var py: AVPlayer?
private var pyContext = 0
...
guard let url = URL(string: "https:// .. /test.m4v") else { return }
py = AVPlayer(url: url)
someNode.geometry?.firstMaterial?.diffuse.contents = py
py?.currentItem?.addObserver(self,
forKeyPath: "presentationSize",
context: &pyContext)
...
override func observeValue(forKeyPath keyPath: String?,
of object: Any?,
change: [NSKeyValueChangeKey : Any]?,
context: UnsafeMutableRawPointer?) {
if context == &py1Context && keyPath == "presentationSize" {
print("Found it ...")
guard let item = object as AVPlayerItem else { return }
let ps = item.presentationSize
let aspect: Float = Float(ps.width) / Float(ps.height)
someNode.geometry?.firstMaterial?
.diffuse.contentsTransform =
SCNMatrix4MakeScale( .. , .. , 1)
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,每当您处理视频时,如果流媒体内容的确切大小并不总是相同,那么对视频进行整形将是一个巨大的痛苦。当然,您有很多考虑因素,例如是否加黑框等等。在一些简单的情况下,计算如下所示:
// 1. You nave finally received the info on the video from the HLS stream:
let ps = item.presentationSize
let spect: Float = Float(ps.width) / Float(ps.height)
// 2. Over in your 3D code, you need to know the current facts on the mesh:
let planeW = ... width of your mesh
let planeH = ... height of your mesh
let planeAspect = planeW / planeH
Run Code Online (Sandbox Code Playgroud)
您可能正在使用 Apple 提供的简单的平面方形网格,例如
var simplePlane = SCNPlane()
simplePlane.width = 2.783
simplePlane.height = 1.8723
Run Code Online (Sandbox Code Playgroud)
无论哪种方式,您都需要网格的宽度/高度。进而,
// 3. In many (not all) cases, the solution is:
let final = vidAspect / planeAspect
print("we'll try this: \(final)")
yourNode.geometry?
.firstMaterial?.diffuse
.contentsTransform = SCNMatrix4MakeScale(1.0, final, 1.0)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1742 次 |
| 最近记录: |