diff options
Diffstat (limited to 'Hotline/Utility/SoundEffects.swift')
| -rw-r--r-- | Hotline/Utility/SoundEffects.swift | 22 |
1 files changed, 12 insertions, 10 deletions
diff --git a/Hotline/Utility/SoundEffects.swift b/Hotline/Utility/SoundEffects.swift index 93f8b9a..74314e4 100644 --- a/Hotline/Utility/SoundEffects.swift +++ b/Hotline/Utility/SoundEffects.swift @@ -1,5 +1,5 @@ -import Foundation import AVFAudio +import Foundation enum SoundEffects: String { case loggedIn = "logged-in" @@ -15,28 +15,30 @@ enum SoundEffects: String { @Observable class SoundEffectPlayer: NSObject, AVAudioPlayerDelegate { static let shared = SoundEffectPlayer() - + private var activeSounds: [AVAudioPlayer] = [] - + func playSoundEffect(_ name: SoundEffects) { // Load a local sound file - guard let soundFileURL = Bundle.main.url( - forResource: name.rawValue, - withExtension: "aiff" - ) else { + guard + let soundFileURL = Bundle.main.url( + forResource: name.rawValue, + withExtension: "aiff" + ) + else { return } - + DispatchQueue.main.async { [weak self] in guard let self = self else { return } - + if let soundEffect = try? AVAudioPlayer(contentsOf: soundFileURL) { soundEffect.delegate = self soundEffect.volume = 0.75 soundEffect.play() - + self.activeSounds.append(soundEffect) } } |