Sound works only on the device, but not in the simulator

I play a few short sounds on my iPad like this: Listen to short sound in iOS

I am using a caf file that I can successfully play from Finder. Now I have gone through quite a bit of trouble trying to achieve sound reproduction, and I'm curious what problems there may be that I do not seem to understand:

  • Option 1: When I create a SystemSoundID and then play it right away, I donโ€™t hear anything on the device and the simulator.

  • Option 2: When I create an instance variable for SystemSoundID and initialize it in viewDidLoad , I can play the sound, but only on the iPad, not on the Simulator.

  • Option 3: Instead of using SystemSoundID I can also use AVAudioPlayer to play the .wav file, which then works on both iPad and Simulator, but here I need to create AVAudioPlayer in viewDidLoad otherwise I wonโ€™t get any sound if I do it all in one time.

The best option currently looks like Option 3 , because it works on both the Simulator and the iPad, but since I need to initialize the Player first, I need an AVAudioPlayer instance for every other sound I want a game that doesn't seem to be very wise memory ...

Is there something Iโ€™m missing and is it possible to play sounds on both platforms using the AudioToolbox framework (Option 1 and Option 2)

+2
ios objective-c ipad audio audiotoolbox
Apr 27 2018-12-12T00:
source share
1 answer

I wrote a library to simplify all this. It wraps AVAudioPlayer and works great on both the device and the simulator.

https://github.com/nicklockwood/SoundManager

The code is pretty simple, although I do some semi-smart things to initialize the audio player. If you do not want to use the library, you can simply copy the code.

A word of warning though - the simulator throws some odd exceptions inside, whenever you use AVAudioPlayer. They do not affect the application at all, but if you included exceptions for exceptions in Xcode, then the application will start in the debugger several times during startup, and you will have to manually resume the work, which can make you crazy if you do not expect this.

+3
May 2 '12 at 16:18
source share



All Articles