Hi all, fairly new to SwiftUI, I'm following a sound manager class recipe, and it works fine if I just want to play one sound at a time, but now in the app I'm working on I need the sounds to overlap each other sometimes, and the singleton I'm using can't seem to do that. I'm not sure if it's the instance of the class, or the instance of the AVAudioPlayer that's the problem. Here is the code I'm using...
import Foundation
import AVKit
class SoundManager {
static let instance = SoundManager()
var player: AVAudioPlayer?
func playMySound() {
guard let url = Bundle.main.url(forResource: "mySound", withExtension: ".wav") else { return }
do {
player = try AVAudioPlayer(contentsOf: url)
player?.play()
} catch let error {
print("Error playing sound. \(error.localizedDescription)")
}
}
// each additional sound is another func like above.
I've poked around on SO and other places looking for better code examples, but nothing I have found works. Any help would be appreciated! Thanks!