I'm trying to play some simple interface sound effects, and the method I found online does not work. I don't get any errors but the sounds just don't play. My phone is not on silent mode, the volume is all the way up.
if let soundUrl = Bundle.main.url(forResource: filename, withExtension: ext) {
var soundId: SystemSoundID = 0
AudioServicesCreateSystemSoundID(soundUrl as CFURL, &soundId)
AudioServicesPlaySystemSoundWithCompletion(soundId, {
AudioServicesDisposeSystemSoundID(soundId);
});
}
I also tried adding this but it did not help:
AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
Any ideas?
Okay found the answer to this. In my settings, Ringers and Alerts has its own volume which apparently is independent of the silent switch and the volume. Mine was set to zero. I went to
settings
>sounds
>Ringer and alerts and increased the volume and it worked.
My app uses SpriteKit and before I figured this out, I found that SKAction.playSoundFileNamed(sound, waitForCompletion: false) will also play a sound. So I might just use that since it does not rely on the Ringers and Alerts volume control.