Search code examples
ios4core-audioavaudioplayeropenal

Include simple sound in iphone app


I searched many questions - but no one seems to be giving simplest, most uniform approach, hence please do not close as duplicate.

My requirement is simple: I have quiz app.

I want to include:

  1. background music that plays continually - probably more than one audio.
  2. I need occassional sounds played at specific events - they are very short in duration. Maybe 4-5 in number.

What sound format do I use? [aac etc]

How do I produce it? (optionally, get it from internet, if free)

What is the best approach to incorporate it? [audioplayback, openal etc)

Forgive me if this is quite stupid, but I am going very generic here and can't seem to find it.

Thanks for the help!


Solution

  • For sound format, use AAC or uncompressed 16-bit little endian in a CAF container (avoid mp3 since it's difficult to make it loop cleanly). You can convert using the command line tool 'afconvert':

    Compressed:

    afconvert -f caff -d aac sourcefile.wav destfile.caf
    

    Uncompressed 16-bit:

    afconvert -f caff -d LEI16 sourcefile.wav destfile.caf
    

    For production, either record it yourself (using an audio program such as Audacity), get a professional to do it, or buy royalty free sounds/music.

    To incorporate it, use AVAudioPlayer for music and OpenAL for sounds. OpenAL is difficult to use and doesn't decode compressed audio on its own, so you may want to use an audio library such as https://github.com/kstenerud/ObjectAL-for-iPhone