SwiftUI sound manager singleton that can play two sounds at once?

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!

Hello,

but now in the app I'm working on I need the sounds to overlap each other sometimes

To get the audio from each sound you are playing to mix, you need one AVAudioPlayer per sound. In your current code, there is always a single AVAudioPlayer, and so there will be no mixing. Instead, you might decide to create and store a new AVAudioPlayer when you call your function.

SwiftUI sound manager singleton that can play two sounds at once?
 
 
Q