Search code examples
objective-caudioavaudioplayeropenal

What is the best approach for audio in a simple iOS game?


I'm new to Objective-C and am very confused about the proper way to handle basic audio. I have a simple game complete but have been fighting with sound issues for a few days.

Here is my understanding:

AVAudioPlayer: Seems ideal for playing a longer sound, for example theme music overlaying a game.

AudioServicesPlaySystemSound: Seems ideal for quick sound effects, less than 30 seconds.

Naturally I would proceed thinking I'll use AVAudioPlayer for theme music and SystemSound for my sound effects. Unfortunately, I immediately noticed a very annoying problem - the two are controlled by different volume sources (SystemSoundServices uses ringer volume??) and generally don't play nice together. This cannot be the setup that most apps use.

I then scrapped the AudioServicesPlaySystemSound approach and reprogrammed everything in AVAudioPlayer. This worked fine for volume but caused terrible lag and could not process a quick sequence of sounds. This does not appear to be useful for sound effects.

I'm now looking into OpenAL, which seems like SERIOUS overkill for my simple game. So could somebody please enlighten me - am I missing something or is OpenAL the best way to approach this common music/sfx game format?


Solution

  • If "there's a lag" when you use AVAudioPlayer then perhaps you are doing it wrong.

    However, in any case I would suggest that you consider AVAudioEngine. Its purpose is exactly to let you do the sort of thing OpenAL does but with a much easier programming interface. It's perfect for a simple game and its sound effects. The WWDC 2014 video on AVAudioEngine shows it being used for exactly that.