Travis
Travis

Reputation: 3369

SKVideoNode only rendering in SCNScene when the node or camera moves

I am using a very simple method to setup a SKVideoNode and place it inside an SCNNode via the geometry's diffuse contents. When I do this, the only time the texture updates and shows the video properly is when the camera or node is moving. When both are stationary, the texture never updates (like the video isn't even playing) but the sound does play.

Obviously it's still playing the video, but not rendering properly. I have no idea why.

func setupAndPlay() {

    // create the asset & player and grab the dimensions
    let path = NSBundle.mainBundle().pathForResource("indycar", ofType: "m4v")!
    let asset = AVAsset(URL: NSURL(fileURLWithPath: path))
    let size = asset.tracksWithMediaType(AVMediaTypeVideo)[0].naturalSize

    let player = AVPlayer(playerItem: AVPlayerItem(asset: asset))

    // setup the video SKVideoNode
    let videoNode = SKVideoNode(AVPlayer: player)
    videoNode.size = size
    videoNode.position = CGPoint(x: size.width * 0.5, y: size.height * 0.5)

    // setup the SKScene that will house the video node
    let videoScene = SKScene(size: size)
    videoScene.addChild(videoNode)

    // create a wrapper ** note that the geometry doesn't matter, it happens with spheres and planes
    let videoWrapperNode = SCNNode(geometry: SCNSphere(radius: 10))
    videoWrapperNode.position = SCNVector3(x: 0, y: 0, z: 0)

    // set the material's diffuse contents to be the video scene we created above
    videoWrapperNode.geometry?.firstMaterial?.diffuse.contents = videoScene
    videoWrapperNode.geometry?.firstMaterial?.doubleSided = true

    // reorient the video properly
    videoWrapperNode.scale.y = -1
    videoWrapperNode.scale.z = -1

    // add it to our scene
    scene.rootNode.addChildNode(videoWrapperNode)

    // if I uncomment this, the video plays correctly; if i comment it, the texture on the videoWrapperNode only 
    // get updated when I'm moving the camera around. the sound always plays properly.
    videoWrapperNode.runAction( SCNAction.repeatActionForever( SCNAction.rotateByAngle(CGFloat(M_PI * 2.0), aroundAxis: SCNVector3(x: 0, y: 1, z: 0), duration: 15.0 )))

    videoNode.play()
}

Has anyone come across anything similar? Any help would be appreciated.

Upvotes: 3

Views: 915

Answers (3)

Morten J
Morten J

Reputation: 914

I also found that setting rendersContinously to true on the renderer (e.g. scnView) will make the video play.

If you log the SCNRendererDelegate's update calls, you can see when frames are drawn

Upvotes: 1

ronsinda
ronsinda

Reputation: 139

I have been unsuccessful & getting a SKVideoNode to display video in Xcode 7.0 or 7.1. If I run my code or other samples on a hardware device like an iPad or iPhone the video display fine, but on the simulator only audio plays. Same code works fine in xCode 6.4's simulator.

I have the CatNap example from Ray Wenderlich iOS & tvOS Games by tutorial (iOS 9) & it does NOT run in the Simulator 9.1 that comes with Xcode 7.1. I believe the simulator is broken & have filed a bug with Apple but have had no response in a month.

Does anyone have sample code for a SKVideoNode the works on the simulator in xCode 7.1??

Upvotes: 0

lock
lock

Reputation: 2897

Sounds like you need to set .playing = true on your SCNView.

From the docs.

If the value of this property is NO (the default), SceneKit does not increment the scene time, so animations associated with the scene do not play. Change this property’s value to YES to start animating the scene.

Upvotes: 5

Related Questions