8

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

Jim T
  • 2,480
  • 22
  • 21
  • 1
    possible duplicate of [Sound not working in iPhone Simulator?](http://stackoverflow.com/questions/302399/sound-not-working-in-iphone-simulator) – brainless coder Jul 25 '14 at 20:55
  • You should be asserting (i.e. assert(soundID != 0) ) after setting that sound, to insure its been set. Also AudioServicesCreateSystemSoundID returns an OSStatus, println it. – David H Jul 25 '14 at 22:14
  • I have tweaked my system sound settings and midi settings as per suggestions sound-not-working-in-iphone-simulator answer. Only thing I couldn't try was setting line-in for input, option doesn't exist (used mic instead). – Jim T Jul 26 '14 at 00:18
  • Have checked soundID, am getting a non 0 value (4097) – Jim T Jul 26 '14 at 00:19
  • Progress - I was testing with my main app after trying the sound setting changes (and restart). Just checked with my test app (only does sound check) and it is now working. But same call in my main app doesn't. – Jim T Jul 26 '14 at 00:27
  • More info - The test app works with iphone 5s ios7 but does not work with iphone 5s ios8. My main app is ios 8 only. Looks like a bug in ios8 simulator code. – Jim T Jul 26 '14 at 00:38

2 Answers2

7

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

Community
  • 1
  • 1
Jim T
  • 2,480
  • 22
  • 21
  • 3
    I confirm that AudioServicesPlaySystemSound() does not work as in iOS 7.1. No sound is played in the simulator, it works though in the actual device. (Tested on Xcode 6 GM) – John Sep 11 '14 at 20:34
  • 2
    AudioServicesPlaySystemSound() does not work for me in the simulator. It does work on the device. Tested with Xcode 6.0.1 and iOS 8.0. – Peter Fennema Sep 22 '14 at 08:56
  • Hi JimT, can you clarify what you with "AudioUnit output - works" and "AVAudioPlayer - works"? thanks – Brabbeldas Apr 03 '15 at 11:41
  • Brabbeldas, Using those APIs I was able to get sound out of the simulator. – Jim T Apr 04 '15 at 16:26
  • Wow. If I could have every hour back that I've wasted on Apple's abandoned APIs. I can't believe they've actually made me prefer Java libraries to their barely-maintained garbage. AudioServicesPlaySystemSound still isn't working in the simulator in iOS 9. – Erik Reppen Jun 20 '16 at 23:53
0

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) -

    let fileURL = NSURL(fileURLWithPath: "/Users/<Home dir>/Desktop/yoursund.mp3")
Avi
  • 2,196
  • 18
  • 18
  • I edited my question to make the file local to the app and print verification that it found the file. The problem still persists – Jim T Jul 25 '14 at 21:20