Just remembered I've done something similar before. Here's the code:
if let soundURL = Bundle.main.url(forResource: soundFile, withExtension: "m4a") {
do {
if debug { print("Playing now.") }
alarmSound = try AVAudioPlayer(contentsOf: soundURL)
if let player = alarmSound {
player.prepareToPlay()
player.play()
} else {
if debug { print("Could not load alarm sound file!") }
}
} catch {
if debug { print(error.localizedDescription) }
}
}
In general, I would also add print statements (or logging using Logger) to all the places things went the wrong way. That way you can more easily identify what is the actual issue.
Topic:
Developer Tools & Services
SubTopic:
Xcode
Tags: