1

I'm building a camera app. I want to create a custom camera click sound, and have it always played when a photo is captured.

Apple's documentation of kAudioServicesPropertyIsUISound says that setting the value to 0 should allow the System Sound to play regardless of the user's preferences.

Note that I am not trying to do this via AVAudioSession, because that sound is affected by the user's volume setting. The goal is for this to be a System Sound via AudioServicesSystemSound.

With the following approach, the system sound plays when the phone is not on silent, but it does NOT play when the phone is on silent.

let customSoundUrl = Bundle.main.url(forResource: "cameraclick", withExtension: ".wav")!
var customSoundId : SystemSoundID = 0
AudioServicesCreateSystemSoundID(customSoundUrl as CFURL, &customSoundId)
var flag: UInt32 = 0
let err = AudioServicesSetProperty(kAudioServicesPropertyIsUISound,
                                   UInt32(MemoryLayout.size(ofValue: customSoundId)),
                                   &customSoundId,
                                   UInt32(MemoryLayout.size(ofValue: flag)),
                                   &flag)

Am I incorrectly setting the value of kAudioServicesPropertyIsUISound? Or am I misinterpreting Apple's documentation, and is it actually not possible to play a System Sound even when the device is on silent?

Plato
  • 111
  • 2
  • 7

0 Answers0