I want to start talking about how to output audio on iPhone devices. Playing audio on iPhones comes a bit like the arcade apothegm: “easy to learn but difficult to master”. Before you start playing audio you should probably know about some core stuff. In this article I try to introduce some of these basics you should know about. Hopefully with success.
First I have to say this article is based on this one.
A quick way to force the iPhone doing sound or music stuff is to use the audio system services. To use them in your xcode project simply add the AudioToolbox Framework to your application and import the header files:
1 | #import <AudioToolbox/AudioToolbox.h> |
to load and play the sound :
1 2 3 4 5 |
Allthough this is good enough to make some UI beeps and blobs you probably want control on preloading files and play them synchronously to an action. On the other hand you want to keep your code as portable as possible? So this seems really like you want to know more about openAL, so lets go through a super quick introduction to it.
There are five consistently appearing terms openAL you may want to know more about :
- LISTENER
- SOURCE
- BUFFER
- DEVICE
- CONTEXT or SESSION
The LISTENER is you. Any sound the listener can ‘hear’ comes out the speakers. OpenAL allows you to specify to place the listener in relation to sources.
The SOURCE is something analogous to the speaker. It generates sound which the listener can ‘hear’. Like the listener, you can move the sources around.
The BUFFER contains the raw audio data of the sound that will be played.
The DEVICE is the actual hardware playing the sound.
The CONTEXT or SESSION is something like a room containing all the sources and the listener. Metaphorically speaking the sources are musicians the listener is a microphone and the CONTEXT is something like air the sound is going through. You will setup positions for listener and sources here and you can specify a category for them:
- kAudioSessionCategory_UserInterfaceSoundEffects:Use this category for sound effects such as touch feedback, explosions, etc.
- kAudioSessionCategory_AmbientSound : Use this category for backgound sounds such as rain, car engine noise, etc. Mixes with other music.
- kAudioSessionCategory_SoloAmbientSound : Use this category for backgound sounds. Other music will stop playing.
- kAudioSessionCategory_MediaPlayback : Use this category for music tracks.
- kAudioSessionCategory_LiveAudio : Use this category for interactive music such as playing an instrument on the screen.
- kAudioSessionCategory_RecordAudio : Use this category when recording audio.
- kAudioSessionCategory_PlayAndRecord : Use this category when recording and playing back audio.
To get this all work you need five tasks to be done:
- get the device
- make a context with the device
- put some data into a buffer
- attach the buffer to a source
- play the source
Audioformats I successfully tested on iPhones OS 2.2.1 and above:
- .CAF (16-Bit PCM) Not Compressed
- .AIF (IMA4 16-Bit PCM) Compressed
.CAF
To convert a audiofile to this format simply open a terminal on your mac and type:
GeSHi Error: GeSHi could not find the language console (using path /var/kunden/webs/anima/anima-entertainment.de/wp/wp-content/plugins/codecolorer/lib/geshi/) (code 2)
.AIF
To convert an audiofile to this format use export on quicktime pro.
Since this was a quick walkthrough I guess you want to hear more about this stuff and you want to see actual code. Next post will give you all this – be prepared!
