From the question in this If I assign a sound in Reality Composer, can I stop it programmatically in RealityKit?, I would like to use method to resume playback after Play Music.
Can I do that?
Now, I use this command in stopAudio function to stop the music.
func stopAudio() {
if arView.scene.anchors.count > 0 {
if arView.scene.anchors[0].isAnchored {
arView.scene.anchors[0].children[0].stopAllAudio()
}
}
}
If I want arView.scene.anchors[0] to replay the music again, which command should I use?
Spatial Audio Playback in RealityKit
Since RealityKit for iOS isn't able to control parameters of Reality Composer 1.5 behaviors, the best strategy for controlling audio is to create a programmatic AudioPlaybackController. To feed your audio file to the controller, export
.rcprojectscene to.usdzformat and use unzipping trick to extract the.aiff,.cafor.mp3sound file. When loading audio for playback, you can choose betweenspatialandnon-spatialaudio experience.In this answer you'll find solutions on how to control Spatial Audio in
iOSandvisionOS.UIKit version
SwiftUI iOS version
SwiftUI visionOS version