I am trying to play a system sound. Using Xcode 6 Beta 4. Here is the code:
func playKeyClick()
{
let filePath = NSBundle.mainBundle().pathForResource("Tock", ofType: "aiff")
if NSFileManager.defaultManager().fileExistsAtPath(filePath) {
println("file exists")
}
let fileURL = NSURL(fileURLWithPath: filePath)
var soundID:SystemSoundID = 0
AudioServicesCreateSystemSoundID(fileURL, &soundID)
AudioServicesPlaySystemSound(soundID)
}
It works fine on devices but does not play in the simulator. I have created a test app that does nothing but play this sound so I don't think I'm doing anything to stop it.
The simulator plays sound from the Safari app ok, so I assume that shows it is capable of playing sound from my app.
Note that I also have other code for playing sound in two other ways that also work on devices but not simulator. I chose this example for simplicity.
Edit - changed file to resource bundle to avoid confusion
I am answering my own question to clarify what I have learned. There were three issues that prevented my original code from working. Note I'm using Xcode6 Beta4, and iphone 5s on the simulator.
1- The original file path I was using of "/System/Library/Audio/UISounds/Tone.caf" does not work in the simulator, it is only valid on the device. Corrected that by adding audio file to the app.
2- Sound not working in iPhone Simulator? provided information that got sound working for ios 7.1 and earlier. Not sure which of the several suggestions worked since I was testing with ios8 as I ran through them.
3- Because the same code that works on ios7 doesn't work on ios8, I am assuming there is a bug in the io8 simulator code (ios8 device works OK). Out of three ways I have tried to produce sound, only one doesn't work:
AudioServicesPlaySystemSound() - doesn't work
AudioUnit output - works
AVAudioPlayer - works
Because file does not exist at the path you specified. To test this, save an sound file on your local and specify its path your code (See e.g. below) -