Recording Audio
Capture microphone input to a file.
Recording Audio is a free Swift Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 4 lessons in total.
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.
Learn Swift with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 122
- Lessons
- 409
Frequently Asked Questions
Is the “Recording Audio” lesson free?
Yes — any 3 lessons of the Swift Academy learning path, including “Recording Audio”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 4 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.
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.