Skip to content

Instantly share code, notes, and snippets.

@gallaugher
Last active June 26, 2025 14:09
Show Gist options
  • Save gallaugher/5dd71084c65c1e4c7f575a8766b4e816 to your computer and use it in GitHub Desktop.
Save gallaugher/5dd71084c65c1e4c7f575a8766b4e816 to your computer and use it in GitHub Desktop.
playSound Function in SwiftUI
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