2 Replies
      Latest reply on Jul 7, 2018 7:00 AM by john craig
      john craig Level 1 Level 1 (0 points)
        //  ViewController.swift
        
        import UIKit
        import SceneKit
        import ARKit
        
        class ViewController: UIViewController, ARSCNViewDelegate {
        
            @IBOutlet var sceneView: ARSCNView!
        
        //JCF Audio
            /// Source for audio playback
            var audioSource: SCNAudioSource!
            
            /// Contains the virtual object that is placed in the real world
            var objectNode: SCNNode!
        //JCF End Audio
            
            override func viewDidLoad() {
                super.viewDidLoad()
                
                // Set the view's delegate
                sceneView.delegate = self
                
                // Show statistics such as fps and timing information
                sceneView.showsStatistics = true
                
                // Create a new scene
                let scene = SCNScene(named: "art.scnassets/Virtual_San_Francisco_Painted_Ladies.scn")!
                
                // Set the scene to the view
                sceneView.scene = scene
        
        //JCF Audio
                // Set up object node
                objectNode = SCNNode()
                // Set up audio playback
                setUpAudio()
        //JCF End Audio
                
            }
            
            override func viewWillAppear(_ animated: Bool) {
                super.viewWillAppear(animated)
                
                // Prevent the screen from being dimmed to avoid interuppting the AR experience.
                UIApplication.shared.isIdleTimerDisabled = true
        
                // Create a session configuration
                let configuration = ARWorldTrackingConfiguration()
        
                // Run the view's session
                sceneView.session.run(configuration)
        
        //JCF End Audio
                playSound()
        //JCF End Audio
        
            }
            
            override func viewWillDisappear(_ animated: Bool) {
                super.viewWillDisappear(animated)
        
        //JCF Audio
               // Stop the audio
                objectNode.removeAllAudioPlayers()
        //JCF END Audio
        
                // Pause the view's session
                sceneView.session.pause()
            }
            
            override func didReceiveMemoryWarning() {
                super.didReceiveMemoryWarning()
                // Release any cached data, images, etc that aren't in use.
            }
        
            // MARK: - ARSCNViewDelegate
            
        /*
            // Override to create and configure nodes for anchors added to the view's session.
            func renderer(_ renderer: SCNSceneRenderer, nodeFor anchor: ARAnchor) -> SCNNode? {
                let node = SCNNode()
             
                
                //JCF Audio
                // Play a positional environment sound layer from the newly placed object
                playSound()
                //JCF END Audio
                
                return node
            }
        */
            
            func session(_ session: ARSession, didFailWithError error: Error) {
                // Present an error message to the user
                
            }
            
            func sessionWasInterrupted(_ session: ARSession) {
                // Inform the user that the session has been interrupted, for example, by presenting an overlay
                
            }
            
            func sessionInterruptionEnded(_ session: ARSession) {
                // Reset tracking and/or remove existing anchors if consistent tracking is required
                
            }
        
        //JCF Audio
            /// Sets up the audio for playback.
            private func setUpAudio() {
                // Instantiate the audio source
                audioSource = SCNAudioSource(fileNamed: "art.scnassets/Etoria_Cheeks.mp3")!
                // As an environmental sound layer, audio should play indefinitely
                audioSource.loops = true
                // Decode the audio from disk ahead of time to prevent a delay in playback
                audioSource.load()
            }
            /// Plays a sound on the `objectNode` using SceneKit's positional audio
            private func playSound() {
                // Ensure there is only one audio player
                objectNode.removeAllAudioPlayers()
                // Create a player from the source and add it to `objectNode`
                objectNode.addAudioPlayer(SCNAudioPlayer(source: audioSource))
            }
        //End JCF Audio
        
        }