0

I would like to play individual audio files from reference images.

Would I use a lazy var to load the audio file and then use

func getNode(withImageName name: String) 

to trigger which audio to play?

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
skynard
  • 133
  • 5

1 Answers1

1

Yes, you can.

If you wanna know how to use SceneKit positional audio read this post.

In case you use RealityKit positional audio in your app – read this post.


Let's see how your code may look like for lazy var:

 lazy var geoAndAudioNode: SCNNode = {
    
    guard let scene: SCNScene = SCNScene(named: "pixar.usdz"),
          let node: SCNNode = scene.rootNode.childNode(withName: "model", 
                                                    recursively: true)
    else { return SCNNode() }
    
    let myPath = Bundle.main.path(forResource: "audio", ofType: "mp3")
    let myURL = URL(fileURLWithPath: myPath!)
    let mySource = SCNAudioSource(url: myURL)!
    
    let audioNode = SCNNode()
    let player = SCNAudioPlayer(source: mySource)
    node.addChildNode(audioNode)
    audioNode.addAudioPlayer(player)
    
    return node
}()

And here's how your code may look like inside renderer method:

extension ViewController: ARSCNViewDelegate {
    
    func renderer(_ renderer: SCNSceneRenderer,
                 didAdd node: SCNNode,
                  for anchor: ARAnchor) {
        
        guard let imageAnchor = anchor as? ARImageAnchor,
              let imageName = imageAnchor.referenceImage.name
        else { return }
                    
        let geometryNode = retrieveNode(name: imageName)
        node.addChildNode(geometryNode)
    }
    
    func retrieveNode(name: String) -> SCNNode {
        
        var node = SCNNode()
        
        switch name {
            case "geoAndAudioImage": node = geoAndAudioNode
            default: break
        }
        return node
    }
}
Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
  • 1
    I was able to stumble through it with similiar code and some tutorials. Much to learn and the posts will be helpful as well. – skynard Aug 06 '20 at 21:24
  • 1
    Should I comment on the post for scenekit positional audio here or click on the post first and then comment? – skynard Aug 09 '20 at 22:17
  • It's up to you)) – Andy Jazz Aug 10 '20 at 07:41
  • Trying out the code from your post on scenekit positional audio. Getting "Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value" on the line – skynard Aug 10 '20 at 13:31
  • 1
    "let myURL = URL(fileURLWithPath: myPath!)" Seems as if it can't find the audio even though I loaded an audio file that plays in other projects that I have created. – skynard Aug 10 '20 at 13:40
  • Maybe your audio format is unreadable inside SceneKit? – Andy Jazz Aug 10 '20 at 13:53
  • Is readable. I am using it in a different instance and plays fine. I haven't used bundle before to play audio but is mono. file names seem correct. – skynard Aug 10 '20 at 15:36
  • 1
    let myPath = Bundle.main.path(forResource: "hail Mary prayer mono", ofType: "mp3") let myURL = URL(fileURLWithPath: myPath!) let mySource = SCNAudioSource(url: myURL)! – skynard Aug 10 '20 at 15:39
  • 1
    Please send me this file via FilesSharing resource. I'll test it. – Andy Jazz Aug 10 '20 at 15:54
  • Not sure where to upload the file. I am assuming not PasteBin? – skynard Aug 10 '20 at 20:19
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/219573/discussion-between-skynard-and-andy-fedoroff). – skynard Aug 10 '20 at 21:02
  • dropmefiles.com – Andy Jazz Aug 10 '20 at 21:22
  • 1
    https://www.dropbox.com/sh/a1jbla0z9146fsp/AADNEFb3qtuzecwjrmK2FU-6a?dl=0 – skynard Aug 10 '20 at 21:48
  • Thank you, I'll tell you about result. – Andy Jazz Aug 11 '20 at 05:13
  • 1
    Thank you!! The avaudiomixing looks like a great tool to really control audio. Hopefully it lives up to it. – skynard Aug 11 '20 at 13:42
  • AURemoteIO.cpp:1590:Start: AUIOClient_StartIO failed (561015905) 2020-08-11 16:28:52.940425-0700 wire audio[1267:753773] [avae] AVAEInternal.h:109 [AVAudioEngineGraph.mm:1544:Start: (err = PerformCommand(*ioNode, kAUStartIO, NULL, 0)): error 561015905 – skynard Aug 11 '20 at 23:33
  • I see the sphere moving but audio not playing – skynard Aug 11 '20 at 23:33
  • 1
    I had the audio set to 0.0 I apologize for such stupidity. rookie. – skynard Aug 11 '20 at 23:41