Trouble Getting RealityKit audio to play

I can't figure out how to get audio from my RealityKitContentBundle to play on Vision Pro...

I have a scene in Reality Composer Pro called "WinterVivarium" which contains a 3D model of a tree, a particle emitter, a ChannelAudio entity, and an audio file (m4a) with 30 minutes of nature sounds.

The 3D model and particle emitter load up just fine on my device, but I'm getting an error when I try to load the audio...

Swift file below. When I run the app and this file gets called it throws the following error:

"Error loading winter vivarium model and/or audio: The operation couldn’t be completed. (RealityKit.__REAsset.LoadError error 2.)"

ChatGPT tells me error code 2 likely means "file not found" but I'm not sure on that one...

Please help!

import SwiftUI
import RealityKit
import RealityKitContent

struct WinterVivarium: View {
    
    @State private var angle: Angle = .degrees(0)
    
    var body: some View {
    
        RealityView { content in
            let audioFilePath = "/Root/back-yard-feb-7am.m4a"
            let audioEntity = Entity()
            
            do {
                let entity = try await Entity(named: "WinterVivarium", in: realityKitContentBundle)
                content.add(entity)
                let resource = try await AudioFileResource.load(named: audioFilePath, from: "WinterVivarium.usda", in: RealityKitContent.RealityKitContentBundle)
                
                let audioController = audioEntity.playAudio(resource)
            } catch {
                print("Error loading winter vivarium model and/or audio: \(error.localizedDescription)")
            }
        }
        
        
    }



#Preview {
    WinterVivarium()
}
Answered by adamkendall1 in 793403022

After 4 long months I finally got my audio playing!!!

I used the method Dilmer Valecillos describes in his YouTube video here: https://www.youtube.com/watch?v=eMA1Vd1nc9M

Skip to @44:40 to let him walk you through the solution.

Here is my working file for reference:

import SwiftUI
import RealityKit
import RealityKitContent

struct WinterVivarium: View {
    
    @State private var audioController: AudioPlaybackController?
    
    var body: some View {
        
        RealityView { content in
            guard let entity = try? await Entity(named: "WinterVivarium", in: realityKitContentBundle) else {
                fatalError("Unable to load WinterVivarium")
            }
            
            let ambientAudioEntity = entity.findEntity(named: "ChannelAudio")
            
            guard let resource = try? await AudioFileResource(named: "/Root/back_yard_feb_7am_m4a", from: "WinterVivarium.usda", in: realityKitContentBundle) else {
                fatalError("Unable to find audio file back_yard_feb_7am_m4a")
            }
            
            audioController = ambientAudioEntity?.prepareAudio(resource)
            audioController?.play()
            
            content.add(entity)
        }
        .onDisappear(perform: {
            audioController?.stop()
        })
    }
}

#Preview {
    WinterVivarium()
}

It seems I can't edit the original post so I'll put this edit here:

The code I posted was incomplete and does not build. Here's the code that actually builds and throws an error.

Error is a little different than originally stated. I guess I fixed something and broke something else. Anyways, here's the current error: "Error loading winter vivarium model and/or audio: File not found at WinterVivarium.usda:/Root/back-yard-feb-7am.m4a."

import SwiftUI
import RealityKit
import RealityKitContent

struct WinterVivarium: View {
    
    @State private var angle: Angle = .degrees(0)
    
    var body: some View {
    
        RealityView { content in
            let audioFilePath = "/Root/back-yard-feb-7am.m4a"
            let audioEntity = Entity()
            
            do {
                let entity = try await Entity(named: "WinterVivarium", in: realityKitContentBundle)
                content.add(entity)
                //await playAudio()
                let audioEmitter = audioEntity.findEntity(named: "ChannelAudio")
                let resource = try AudioFileResource.load(named: audioFilePath, from: "WinterVivarium.usda", in: realityKitContentBundle)
                
                let audioPlaybackController = audioEmitter!.prepareAudio(resource)
                audioPlaybackController.play()
            } catch {
                print("Error loading winter vivarium model and/or audio: \(error.localizedDescription)")
            }
        }
        
        
    }
    
    func playAudio() async {

        let entity = await Entity()
        guard let entity = await entity.findEntity(named: "ChannelAudio"),
                  let resource = try? await AudioFileResource(named:"/Root/back_yard_feb_7am_m4a", from: "WinterVivarium.usda", in: realityKitContentBundle) else { return }

        let audioPlaybackController = await entity.prepareAudio(resource)

        await audioPlaybackController.play()
        }
    }
    




#Preview {
    WinterVivarium()
}

Remove the root from the name and you don't need to include the format.

Try the following:

let audioFilePath = "back-yard-feb-7am"

@gebs Thanks for the reply.

Unfortunately that gives me the same error. The file name appears as "back_yard_feb_7am_m4a" in Reality Composer Pro, but similarly to gb10, I've tried every combination of:

  • with/without "/Root" in the file path
  • with hyphens
  • with underscores
  • with/without file extension

But nothing seems to work. I'm sure I'm missing something very basic here which is preventing my Xcode project from finding the file in the Reality Kit bundle... but I have no idea what that might be.

I was able to get this error to go away eventually. I made a few changes and am not sure which one fixed the issue. I think the thing that ultimately fixed it for me was moving the location of the audio file in Reality Composer Pro. I had the file at the root level, and not in the "Root" folder. To fix this, I dragged and dropped the audio file onto the "Root" folder.

Another issue was that Swift didn't like my ".m4a" format. So after changing the file location, and changing the file type to ".wav", the code below ran without errors. I still can't HEAR the audio being played in my app... so I still have a big problem, but progress was made. If anyone has any other advice I'm all ears.

    var body: some View {
        
        RealityView { content in
            //let audioFilePath = "/Root/back_yard_feb_7am_m4a"
            let audioFilePath = "/Root/Ocean_Sounds_wav"
            let audioEntity = Entity()
            
            do {
                let entity = try await Entity(named: "WinterVivarium", in: realityKitContentBundle)
                content.add(entity)
                
                let audioEmitter = audioEntity.findEntity(named: "ChannelAudio")
                let resource = try AudioFileResource.load(named: audioFilePath, from: "WinterVivarium.usda", in: realityKitContentBundle)
                
                let AudioPlaybackController = audioEmitter?.prepareAudio(resource)
                AudioPlaybackController?.play()
                //Audio.Decibel = 2.0
            } catch {
                print("Error loading winter vivarium model and/or audio: \(error)")
            }
        }
    }
Accepted Answer

After 4 long months I finally got my audio playing!!!

I used the method Dilmer Valecillos describes in his YouTube video here: https://www.youtube.com/watch?v=eMA1Vd1nc9M

Skip to @44:40 to let him walk you through the solution.

Here is my working file for reference:

import SwiftUI
import RealityKit
import RealityKitContent

struct WinterVivarium: View {
    
    @State private var audioController: AudioPlaybackController?
    
    var body: some View {
        
        RealityView { content in
            guard let entity = try? await Entity(named: "WinterVivarium", in: realityKitContentBundle) else {
                fatalError("Unable to load WinterVivarium")
            }
            
            let ambientAudioEntity = entity.findEntity(named: "ChannelAudio")
            
            guard let resource = try? await AudioFileResource(named: "/Root/back_yard_feb_7am_m4a", from: "WinterVivarium.usda", in: realityKitContentBundle) else {
                fatalError("Unable to find audio file back_yard_feb_7am_m4a")
            }
            
            audioController = ambientAudioEntity?.prepareAudio(resource)
            audioController?.play()
            
            content.add(entity)
        }
        .onDisappear(perform: {
            audioController?.stop()
        })
    }
}

#Preview {
    WinterVivarium()
}
Trouble Getting RealityKit audio to play
 
 
Q