aboutsummaryrefslogtreecommitdiff
path: root/Hotline/Library/SoundEffects.swift
diff options
context:
space:
mode:
authorDustin Mierau <dustin@mierau.me>2025-11-07 12:44:55 -0800
committerDustin Mierau <dustin@mierau.me>2025-11-07 12:44:55 -0800
commita55318fa8d643160900bec3e6b14e7404c63497a (patch)
treeab6a9eca9a368b35e1490becc70f94ebde6ac271 /Hotline/Library/SoundEffects.swift
parent786a387d58fc66ae20716a9dee46b74dff8e6894 (diff)
Organize Library folder a bit. Update Tracker add/edit sheet design.
Diffstat (limited to 'Hotline/Library/SoundEffects.swift')
-rw-r--r--Hotline/Library/SoundEffects.swift50
1 files changed, 50 insertions, 0 deletions
diff --git a/Hotline/Library/SoundEffects.swift b/Hotline/Library/SoundEffects.swift
new file mode 100644
index 0000000..93f8b9a
--- /dev/null
+++ b/Hotline/Library/SoundEffects.swift
@@ -0,0 +1,50 @@
+import Foundation
+import AVFAudio
+
+enum SoundEffects: String {
+ case loggedIn = "logged-in"
+ case chatMessage = "chat-message"
+ case transferComplete = "transfer-complete"
+ case userLogin = "user-login"
+ case userLogout = "user-logout"
+ case newNews = "new-news"
+ case serverMessage = "server-message"
+ case error = "error"
+}
+
+@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 {
+ 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)
+ }
+ }
+ }
+
+ func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
+ if let i = self.activeSounds.firstIndex(of: player) {
+ self.activeSounds.remove(at: i)
+ }
+ }
+}