There is effectively a problem, no sound.
I tested with to different calls:
func playInputClick() {
var filePath: String?
filePath = Bundle.main.path(forResource: "ButtonTap", ofType: "wav")
let fileURL = URL(fileURLWithPath: filePath!)
var soundID:SystemSoundID = 0
AudioServicesCreateSystemSoundID(fileURL as CFURL, &soundID)
AudioServicesPlaySystemSound(soundID)
}
func playSound() {
let alertSoundPath = URL(fileURLWithPath: Bundle.main.path(forResource: "ButtonTap", ofType: "wav")!) // Bundle.main.url(forResource: "classicAlarm", withExtension: "mp3")!
do {
let audioPlayer = try AVAudioPlayer(contentsOf: alertSoundPath)
audioPlayer.play()
}
catch {
// appData.logger.debug("Error playing sound: \(alertSoundPath)")
}
}:
playSound does not work, while playInputClick() does.
So, I would change your playSound as follows:
func playSound() {
let alertSoundPath = Bundle.main.path(forResource: "classicAlarm", ofType: "mp3")!
let alertSoundURL = URL(fileURLWithPath: alertSoundPath)
var soundID:SystemSoundID = 0
AudioServicesCreateSystemSoundID(alertSoundURL as CFURL, &soundID)
AudioServicesPlaySystemSound(soundID)
}
In my tests, func are outside ContentView.
I moved playSound inside ContentView and got the runtime error (but some sound):
Modifying state during view update, this will cause undefined behavior.
So, please, show complete code so that we can reproduce (as well as the resources).
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags: