Last active
June 26, 2025 14:09
-
-
Save gallaugher/5dd71084c65c1e4c7f575a8766b4e816 to your computer and use it in GitHub Desktop.
playSound Function in SwiftUI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func playSound(soundName: String) { | |
guard let soundFile = NSDataAsset(name: soundName) else { | |
print("😡 ERROR: Could not read file named \(soundName).") | |
return | |
} | |
do { | |
audioPlayer = try AVAudioPlayer(data: soundFile.data) | |
audioPlayer.play() | |
} catch { | |
print("😡 ERROR: \(error.localizedDescription) when trying to create audioPlayer.") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment