ARKIT : SKVideoNode continue playing while node is out of scene, pause is not working

断了今生、忘了曾经 提交于 2019-12-13 03:46:54

问题


I am implementing a demo project using SceneKit and ARKit where i keep camera on some picture and play video from url. Everything works fine but i could not be able to stop previous video and it starts another video. Previous video plays on background. It happens sometimes when i switch image. My implementation is :

@IBOutlet var sceneView: ARSCNView!
var videosNames = [String]()
var player  = AVPlayer()
var videoNode = SKVideoNode()

In viewDidload,

sceneView.delegate = self

// Create a session configuration
    let configuration = ARImageTrackingConfiguration()

    // first see if there is a folder called "ARImages" Resource Group in our Assets Folder
    if let trackedImages = ARReferenceImage.referenceImages(inGroupNamed: "ARImages", bundle: Bundle.main) {

    // if there is, set the images to track
        configuration.trackingImages = trackedImages
        // at any point in time, only 1 image will be tracked
        configuration.maximumNumberOfTrackedImages = 1
    }

    // Run the view's session
    sceneView.session.run(configuration)

The callback method looks like :

     func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {

    // if the anchor is not of type ARImageAnchor (which means image is not detected), just return
    if let _ : Bool = videosNames.contains(anchor.name!) {

    guard let imageAnchor = anchor as? ARImageAnchor, var fileUrlString = Bundle.main.path(forResource:  "baby1", ofType: "mp4") else {return}

    print("didAdd node:\(anchor.name!)")
    //find our video file
    fileUrlString = "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4" // i set the file URL path directly here


    let videoItem = AVPlayerItem(url: URL(fileURLWithPath: fileUrlString))

    self.player.seek(to: CMTime.zero)
    self.player.pause() // Tried to pause the video, set seek time zero. Nothing works. 
    self.videoNode.pause()

    player = AVPlayer(playerItem: videoItem)
    //initialize video node with avplayer
    videoNode = SKVideoNode(avPlayer: self.player)


    //videoNode.removeAllChildren()
    self.player.play()
    //self.videoNode.play()
    // add observer when our player.currentItem finishes player, then start playing from the beginning
    NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: self.player.currentItem, queue: nil) { (notification) in
        self.player.seek(to: CMTime.zero)
        self.player.play()
        //self.videoNode.play()
        print("Looping Video")
    }


    // set the size (just a rough one will do)
    let videoScene = SKScene(size: CGSize(width: 480, height: 360))
    // center our video to the size of our video scene
    self.videoNode.position = CGPoint(x: videoScene.size.width / 2, y: videoScene.size.height / 2)
    // invert our video so it does not look upside down
    self.videoNode.yScale = -1.0
    // add the video to our scene
    videoScene.addChild(self.videoNode)
    // create a plan that has the same real world height and width as our detected image
    let plane = SCNPlane(width: imageAnchor.referenceImage.physicalSize.width, height: imageAnchor.referenceImage.physicalSize.height)
    // set the first materials content to be our video scene
    plane.firstMaterial?.diffuse.contents = videoScene
    // create a node out of the plane
    let planeNode = SCNNode(geometry: plane)
    // since the created node will be vertical, rotate it along the x axis to have it be horizontal or parallel to our detected image
    planeNode.eulerAngles.x = -Float.pi / 2
    // finally add the plane node (which contains the video node) to the added node
    node.addChildNode(planeNode)

    }
}

When any node gets update

   func renderer(_ renderer: SCNSceneRenderer, didUpdate node: SCNNode, for anchor: ARAnchor) {

    if node.isHidden == true {
        print("Node is out of view:\(anchor.name!) :")
        self.player.seek(to: CMTime.zero)
        videoNode.pause()

    } else {
        print("Node is not out of view:\(anchor.name!)")
    }
}

Here i get log when any new video start playing , but i could not stop previously started video which plays in background (only audio) with the new video. It totally messed up my process. If i put camera away from both image, but the sounds keeps playing. Could anyone please help me for the issue ?

Thanks in advance.


回答1:


Node must be removed from ARSCNView's session. Following method does not removed the node from session.

node.removeFromParentNode() 

Only sceneView.session.remove() works perfectly in didUpdate() method.

func renderer(_ renderer: SCNSceneRenderer, didUpdate node: SCNNode, for anchor: ARAnchor) {

    if node.isHidden == true {            
        if let imageAnchor = anchor as? ARImageAnchor {
            sceneView.session.remove(anchor: imageAnchor)
        }
    } else {
        if !isNodeAdded {
            isNodeAdded = true
        }
    }
}


来源:https://stackoverflow.com/questions/57543919/arkit-skvideonode-continue-playing-while-node-is-out-of-scene-pause-is-not-wo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!