SKVideoNode not showing video

706 Views Asked by At

I've been trying to get this code that's pretty much copied/pasted from Apple's documentation to play a local video but seem to be missing something. I've imported AVFoundation and the video into the project. I'm not throwing any errors but no matter how I tweak am unable to see any video (on my device or simulator). I'm sure it's something simple but am clearly missing something. Please help! I'm running the latest version of IOS and Xcode.

var videoNode: SKVideoNode? = {

        guard let urlString = Bundle.main.path(forResource: "Clouds", ofType: "mov") else {
            print("You messed up son")
            return nil
        }

        let url = URL(fileURLWithPath: urlString)
        let item = AVPlayerItem(url: url)
        let player = AVPlayer(playerItem: item)

        return SKVideoNode(avPlayer: player)
    }()


    override func didMove(to view: SKView) {

        addChild(worldNode)

        videoNode?.position = CGPoint( x: frame.midX, y: frame.midY)
        videoNode?.size.width = 1920
        videoNode?.size.height = 1080
        videoNode?.zPosition = 100
        worldNode.addChild(videoNode!)
        videoNode?.play()
    }
2

There are 2 best solutions below

0
On

After searching around awhile I thought I'd try removing the movie then going to Build Phases > Copy Bundle Resources. I then added my movie in this menu and after rebuilding it worked as expected. I'm guessing my copy of the movie into the project originally may have had issues. Anyway maybe this will help someone down the road.

0
On

Step 1 configure set up camera call this method in view didload

func setUpCamera ()
{
    sceneView.delegate = self
    // Do any additional setup after loading the view.
    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 = 8
    }
    // Run the view's session
    sceneView.session.run(configuration,options: [.resetTracking, .removeExistingAnchors])

}

This method is called when you scan image

// MARK: - ARSCNViewDelegate

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



    DispatchQueue.main.async {
        self.btnFullScreen.isHidden = false
    }
    // if the anchor is not of type ARImageAnchor (which means image is not detected), just return
    guard let imageAnchor = anchor as? ARImageAnchor,
        let fileUrlString = Bundle.main.path(forResource: "videoplayback", ofType: "mp4") else {return}
    //find our video file
    let videoItem = AVPlayerItem(url: URL(fileURLWithPath: fileUrlString))
    player = AVPlayer(playerItem: videoItem)
    //initialize video node with avplayer
    let videoNode = SKVideoNode(avPlayer: player)
    player.play()
    // add observer when our player.currentItem finishes player, then start playing from the beginning
    NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: player.currentItem, queue: nil) { (notification) in
        self.player.seek(to: CMTime.zero)
        self.player.pause()
        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
    videoNode.position = CGPoint(x: videoScene.size.width / 2, y: videoScene.size.height / 2)
    // invert our video so it does not look upside down
    videoNode.yScale = -1.0
    // add the video to our scene
    videoScene.addChild(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)
}

enter image description here

it's work like charm for me let me know if you find any difficulties i will help you

This technique called ARkitimage tracking...