diff options
| author | Dustin Mierau <dustin@mierau.me> | 2024-05-01 21:22:18 -0700 |
|---|---|---|
| committer | Dustin Mierau <dustin@mierau.me> | 2024-05-01 21:32:15 -0700 |
| commit | 60a3f2d29dfff945f59787f5a7786c6e78ba7bb9 (patch) | |
| tree | e3d0aa6ae541790ea700353b0ac01fe8b4950f9f /Hotline/Utility/SoundEffects.swift | |
| parent | b9529a0d255ad41c62a4e3b93a47ebb5bb82a565 (diff) | |
New Hotline toolbar window for banner.
Diffstat (limited to 'Hotline/Utility/SoundEffects.swift')
| -rw-r--r-- | Hotline/Utility/SoundEffects.swift | 25 |
1 files changed, 15 insertions, 10 deletions
diff --git a/Hotline/Utility/SoundEffects.swift b/Hotline/Utility/SoundEffects.swift index 175fa5b..931e27f 100644 --- a/Hotline/Utility/SoundEffects.swift +++ b/Hotline/Utility/SoundEffects.swift @@ -12,7 +12,9 @@ enum SoundEffects: String { @Observable class SoundEffectPlayer: NSObject, AVAudioPlayerDelegate { - var activeSounds: [AVAudioPlayer] = [] + static let shared = SoundEffectPlayer() + + private var activeSounds: [AVAudioPlayer] = [] func playSoundEffect(_ name: SoundEffects) { // Load a local sound file @@ -23,18 +25,21 @@ class SoundEffectPlayer: NSObject, AVAudioPlayerDelegate { return } - do { - let soundEffect = try AVAudioPlayer(contentsOf: soundFileURL) - soundEffect.delegate = self - - self.activeSounds.append(soundEffect) + DispatchQueue.main.async { [weak self] in + guard let self = self else { + return + } - soundEffect.volume = 0.75 - soundEffect.play() + if let soundEffect = try? AVAudioPlayer(contentsOf: soundFileURL) { + soundEffect.delegate = self + soundEffect.volume = 0.75 + soundEffect.play() + + self.activeSounds.append(soundEffect) + } } - catch {} } - + func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) { if let i = self.activeSounds.firstIndex(of: player) { self.activeSounds.remove(at: i) |