2

Hello I'm new into developing and right now I'm working on an AR application which works with image tracking and it should play different videos on different tracked images. Right now the video stops, how can I loop the video? Also if the image is not in the camera view, the video continues. Is there a pause function too? I would be thankful for every hints.

import UIKit
import SceneKit
import ARKit

class ViewController: UIViewController, ARSCNViewDelegate {

    @IBOutlet var sceneView: ARSCNView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        sceneView.delegate = self
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        let configuration = ARImageTrackingConfiguration()
        
        if let trackedImages = ARReferenceImage.referenceImages(inGroupNamed: "AR Resources", bundle: Bundle.main) {
            configuration.trackingImages = trackedImages
            configuration.maximumNumberOfTrackedImages = 2
        }
        
        sceneView.session.run(configuration)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        sceneView.session.pause()
    }

    func renderer(_ renderer: SCNSceneRenderer, nodeFor anchor: ARAnchor) -> SCNNode? {
        
        let node = SCNNode()
        
        if let imageAnchor = anchor as? ARImageAnchor {
            
            let size = imageAnchor.referenceImage.physicalSize
            
            var videoNode = SKVideoNode()
            
            switch imageAnchor.name {
            case "Image1":
                videoNode = SKVideoNode(fileNamed: "Image1.mp4")
            case "Image2":
                videoNode = SKVideoNode(fileNamed: "Image2.mp4")
            case "Image3":
                videoNode = SKVideoNode(fileNamed: "Image3.mp4")
            default:
                break
            }
            
            videoNode.play()
                
                
            let videoScene = SKScene(size: CGSize(width: 1280, height: 960))
            videoScene.anchorPoint = CGPoint(x: 0.5, y: 0.5)
            videoScene.addChild(videoNode)
            
            let plane = SCNPlane(width: size.width, height: size.height)
            plane.firstMaterial?.diffuse.contents = videoScene
            let planeNode = SCNNode(geometry: plane)
            plane.firstMaterial?.isDoubleSided = true
            planeNode.eulerAngles.x = .pi / 2
            
            node.addChildNode(planeNode)
            
            return node
        }
        
        return nil
    }
}
aheze
  • 24,434
  • 8
  • 68
  • 125
kevbdnr
  • 21
  • 1
  • Maybe [AVPlayerLooper()](https://stackoverflow.com/a/38271221/15372665) works? You'll need to [initialize the node with AVPlayer](https://developer.apple.com/documentation/spritekit/skvideonode/1407900-init) though. – jp21 May 18 '21 at 20:26
  • 1
    thank you for the hint, but I don't know where and how to implement this in my code so it works... any advice? ^^ – kevbdnr May 21 '21 at 09:17
  • I could only find a [complete example code](https://developer.apple.com/library/archive/samplecode/avloopplayer/Introduction/Intro.html#//apple_ref/doc/uid/TP40014695-Intro-DontLinkElementID_2) for looping video and audio. Sadly, I don't know where exactly to initialize the SKVideoNode :( – jp21 May 22 '21 at 03:53
  • 1
    Oh thank you, no problem, I will try that^^ – kevbdnr May 25 '21 at 10:52

0 Answers0