ios – How to set the aspect ratio of an AVPlayer when attached to a SCNGeometry contents?


I’m creating an SCNPlane in SceneKit for video playback. I’m using an AVPlayer to play the video from a specified URL. Here’s how I do it:

Setting up the plane node:

func setupVideoPlaybackPlaneNodes() {
    guard let scene else { return }
    
    let plane = SCNPlane(width: videoPlaybackPlaneWidth, height: videoPlaybackPlaneHeight)
    plane.firstMaterial?.isDoubleSided = true
    playbackNode = SCNNode(geometry: plane)
    playbackNode?.opacity = videoPlaybackPlaneOpacity

    scene.rootNode.addChildNode(playbackNode!)
}

Setting up the AVPlayer for the node:

func setPlayer() {
    let videoURL = /* The local video URL here */
    let videoPlaybackPlayer = AVPlayer(url: videoURL)
    playbackNode?.geometry?.firstMaterial?.diffuse.contents = videoPlaybackPlayer
}

I can’t seem to find how to set the aspect ratio of the AVPlayer when it’s attached to a SCNGeometry‘s contents. I tried this:

videoPlaybackPlayer?.externalPlaybackVideoGravity = .resizeAspect

But it doesn’t work. Is there any other way to achieve this?

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img