Apple's framework for working with audiovisual media. AVFoundation provides capabilities for audio playback and recording, video capture, editing, and playback on iOS devices.
of iOS users worldwide can use AVFoundation (iOS 4+ required)
| iOS Version | Market Share | Status |
|---|---|---|
| iOS 18.7 | 30.3% | Supported |
| iOS 18.6 | 29.8% | Supported |
| iOS 26.1 | 10.7% | Supported |
| iOS 18.5 | 6.0% | Supported |
| iOS 16.7 | 2.3% | Supported |
| iOS 26.2 | 2.0% | Supported |
| iOS 18.3 | 1.8% | Supported |
| iOS 15.8 | 1.8% | Supported |
| iOS 11.0 | 1.7% | Supported |
| iOS 17.6 | 1.7% | Supported |
Audio recording and playback
Video capture and playback
Media composition and editing
Camera and microphone access
Asset management and export
Real-time media processing
Simple audio playback with AVAudioPlayer
import AVFoundation
class AudioPlayer {
var audioPlayer: AVAudioPlayer?
func playSound(named soundName: String) {
guard let url = Bundle.main.url(forResource: soundName, withExtension: "mp3") else {
print("Sound file not found")
return
}
do {
audioPlayer = try AVAudioPlayer(contentsOf: url)
audioPlayer?.play()
} catch {
print("Error playing sound: \(error)")
}
}
}Record audio from the microphone
import AVFoundation
class AudioRecorder {
var audioRecorder: AVAudioRecorder?
func startRecording() {
let audioFilename = getDocumentsDirectory().appendingPathComponent("recording.m4a")
let settings = [
AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
AVSampleRateKey: 12000,
AVNumberOfChannelsKey: 1,
AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue
]
do {
audioRecorder = try AVAudioRecorder(url: audioFilename, settings: settings)
audioRecorder?.record()
} catch {
print("Could not start recording")
}
}
func stopRecording() {
audioRecorder?.stop()
}
func getDocumentsDirectory() -> URL {
FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
}
}Access and configure the device camera
import AVFoundation
import UIKit
class CameraViewController: UIViewController {
var captureSession: AVCaptureSession?
var previewLayer: AVCaptureVideoPreviewLayer?
override func viewDidLoad() {
super.viewDidLoad()
setupCamera()
}
func setupCamera() {
captureSession = AVCaptureSession()
guard let videoCaptureDevice = AVCaptureDevice.default(for: .video) else { return }
let videoInput: AVCaptureDeviceInput
do {
videoInput = try AVCaptureDeviceInput(device: videoCaptureDevice)
} catch {
return
}
if (captureSession?.canAddInput(videoInput) ?? false) {
captureSession?.addInput(videoInput)
}
previewLayer = AVCaptureVideoPreviewLayer(session: captureSession!)
previewLayer?.frame = view.layer.bounds
previewLayer?.videoGravity = .resizeAspectFill
view.layer.addSublayer(previewLayer!)
captureSession?.startRunning()
}
}Apple's framework for working with audiovisual media. AVFoundation provides capabilities for audio playback and recording, video capture, editing, and playback on iOS devices.
AVFoundation is available on iOS 4 and later. Currently, 99.8% of iOS users worldwide can use this framework.
Related frameworks include Core Audio, Core Media. Each has different capabilities and iOS version requirements.