Recording Audio
Capture microphone input to a file.
Recording Audio is a free Swift Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Recording with AVAudioRecorder
AVAudioRecorder captures audio from the microphone to a file with minimal setup—ideal for voice memos and simple recorders.
import AVFoundationMicrophone Permission
Recording requires user permission. Add NSMicrophoneUsageDescription to Info.plist and request access before recording.
AVAudioApplication.requestRecordPermission { granted in
if granted { /* start recording */ }
}Configuring the Audio Session
Set the session category to .record or .playAndRecord and activate it so the system routes microphone input to your app.
let session = AVAudioSession.sharedInstance()
try session.setCategory(.playAndRecord)
try session.setActive(true)Choosing a File URL
Recordings are written to a file. Build a URL in the documents directory with an appropriate extension like .m4a.
let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let url = dir.appendingPathComponent("memo.m4a")Recorder Settings
Settings define format, sample rate, channels, and quality via a dictionary of AVAudioRecorder keys.
let settings: [String: Any] = [
AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
AVSampleRateKey: 44100,
AVNumberOfChannelsKey: 1,
AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue
]Creating the Recorder
Initialize AVAudioRecorder with the URL and settings, then call prepareToRecord() to allocate resources.
let recorder = try AVAudioRecorder(url: url, settings: settings)
recorder.prepareToRecord()Starting and Stopping
Call record() to begin and stop() to finish. Stopping finalizes and closes the audio file.
recorder.record()
// later
recorder.stop()Timed and Paused Recording
You can record for a fixed duration with record(forDuration:), and pause() / record() to resume into the same file.
recorder.record(forDuration: 30) // stop after 30sMetering Levels
Enable isMeteringEnabled, then call updateMeters() and read averagePower(forChannel:) to drive a live level meter.
recorder.isMeteringEnabled = true
recorder.updateMeters()
let power = recorder.averagePower(forChannel: 0) // in dBThe Delegate
Conform to AVAudioRecorderDelegate to learn when recording finishes successfully or an encode error occurs.
func audioRecorderDidFinishRecording(_ recorder: AVAudioRecorder,
successfully flag: Bool) {
print("finished: \(flag)")
}Playing Back the Result
After recording, play the file with AVAudioPlayer pointed at the same URL—a quick way to verify the capture.
let player = try AVAudioPlayer(contentsOf: url)
player.play()Quick Check: Recording
Test your understanding of audio recording.
Recap: Recording Audio
AVAudioRecorder captures microphone audio to a file after you request permission and activate a recording AVAudioSession. Provide a URL and a settings dictionary (format, sample rate, channels, quality), prepareToRecord(), then record()/stop().
Enable metering for live levels, use the delegate for completion, and play back with AVAudioPlayer to verify.
Frequently asked questions
Is the “Recording Audio” lesson free?
Yes — the full text of “Recording Audio” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.
What will I learn in “Recording Audio”?
Capture microphone input to a file. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Swift Academy?
No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Recording Audio” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Swift Academy lesson?
Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.