Try this. If it doesn't work, it should at least spit out some useful error information in the console at run time.
do {
let entity = try await Entity(named: "Stone", in: realityKitContentBundle)
content.add(entity)
} catch {
print("Error loading stone model: \(error.localizedDescription)")
}
Post
Replies
Boosts
Views
Activity
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()
}
@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)")
}
}
}
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()
}