Iphone – How to record audio on iPhone

iphone

I want to record audio using the iphone (< 2 minutes) and save it to a file.
I looked at SpeakHere, but it confuses me. Which classes do I use? What delegate methods do I create?

Thanks!

Best Answer

I agree, SpeakHere is not a very good starting point to learn iPhone audio.

iPhone audio uses two concepts. AudioQueues, and AudioSessions. If you want to record to a file, you will need to create one AudioSession, activate the session, and create an AudioInputQueue and an AudioOutputQueue.

The reference for AudioQueues (by far the part you will deal with most) is:

http://developer.apple.com/iphone/library/documentation/MusicAudio/Conceptual/AudioQueueProgrammingGuide/Introduction/Introduction.html

As for AudioSessions:

http://developer.apple.com/iphone/library/documentation/Audio/Conceptual/AudioSessionProgrammingGuide/Introduction/Introduction.html

Though you can ignore most of the AudioSession stuff since you won't be doing anything quite that complicated. So basically, here are the steps:

  1. Initialize your audio session with an AudioInterrupt callback. This callback handles the case of an incoming call, interrupting your program.
  2. Setup the data formats for both your incoming audio, and the filebound audio. This is stored in a struct called AudioStreamBasicDescripion.
  3. Create an AudioQueue object and use AudioQueueNewInput to initialize it. You will have to specify a callback to handle incoming audio. This will be where you can specify saving the audio to a file, though beware, this is a real time thread, and you will have to try as best you can not to block it for too long.
  4. Define how many AudioQueueBuffers your recording system will have. These buffers are filled up according to your sampling rate which you specified in step 2. You will have to adjust these so that you have enough time to do your processing before the next buffer arrives.
  5. AudioSessionSetActive(YES);
  6. AudioQueueStart on your AudioQueue.

I didn't include all parameters here, but that's what the API is for.

Hope that helps.

[EDIT]

Sorry, forgot to include the output stuff, though they are fairly straight forward. Create another AudioQueue, initialize with an AudioQueueNewOutput, and the API should be able to guide you the rest of the way.

Cheers.