I can't seem to get basic ambient audio to work in ARKit. Would someone be willing to look at my ViewController.swift and advise? My project is based on the Audio in ARKit example.

//  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

}

Replies

Can you please be more specific? what are you trying to do?
also, are you sure you are using mono and not stereo audio, as described by apple ("For best results, use mono audio files. SceneKit’s audio engine uses panning to create 3D positional effects, so stereo audio sources produce less recognizable 3D audio effects.", https://developer.apple.com/documentation/arkit/creating_an_immersive_ar_experience_with_audio)?

Thank you for your reply henrikfrommutlangen. I am trying to add a simple looping sound wich plays when the user launches the ARkit app. In this case, it will be a voiceover audio file, but it would function similar to an ambient score. Please see this screenrecording as an example. In this screenrecording, the audio was added in video post production, but it should give you an idea.


I am basing the aproach on Apple Developer's Creating an Immersive AR Experience with Audio tutorial. I am modifing a simple place AR scnene on first detected plane at launch, using world tracking method. The rest of the code in the ViewController.swift above is tested and fully functional.


The audio snippets are indicated with a '//JCF Audio ... //JCF End Audio' comment.


The audio file I am using is indeed a mono file, as recommended. I ran a test using the "fireplace.mp3" provided in the tutorial, but it faild to play as well, so I don't think that the problem has anything to do with the file preparation.


Thanks again for your time henrikfrommutlangen.