openframeworks/openFrameworks — pull request #2187 by admsyn (2013-07-08 12:37:12+00:00)
Title: AVFoundation API for iOS Audio Sessions 

This PR migrates the Audio Session management from the C AudioSession API to the AVFoundation API. The C API is deprecated, so this is mostly a future-proofing effort. There shouldn't be any noticeable changes for oF users here (except that this closes issue #2180).

This was tested on an iPad running iOS 6, as well as a simulator running iOS 5. It wasn't tested with iOS 7, though the API I've used was mostly introduced in iOS 6, so in theory it should just work (could use a test anyway, of course).

Crucially, this **has not been tested on an iPhone**. What needs to be tested for sure is that the speaker override on the Audio Session category worked correctly. The AVFoundation API changes the semantics of how to do this, though the documentation makes it sound what I've done is the correct way. See [this part of SoundInputStream](https://github.com/admsyn/openFrameworks/blob/9a370b25104c1e4e408d5d8a67074d065b28c67e/addons/ofxiPhone/src/sound/SoundInputStream.m#L120) for the relevant switch. Running the audioInputExample and triggering the sound clip should be a sufficient test.

This PR also sets the sample rate of the audio session, which was previously just assumed to be 44100 as far as I can tell?

Some notes for @julapy:

* I moved some of the Audio Session config to the SoundStream superclass. Just the stuff that was shared between the two implementations.
* The interruption handling is now also all done in SoundStream, since it was effectively the same for both the input and output stream
* I fixed #2180 by just implementing the `audioPlayerEndInterruption` delegate method. I'm not sure if that was an oversight, or if I've subtly broken something in the process.
* The AVAudioSession API uses NSErrors for communicating session errors (i.e. no need to parse error constants). I've rigged this up to relay messages in a way similar to yours by just calling `localizedDescription` on the errors. I then turned `checkStatus:` into a method to check audio unit errors on the RemoteIO.