Building a podcast player with AVFoundation
Podcasts appear deceptively simple: a list of episodes, a play button, a progress bar and a duration. The difficult part is keeping those elements synchronised while audio streams, downloads, locks, Bluetooth devices and app interruptions compete for control. Building a podcast player app with AVFoundation works best when playback is treated as an independent system rather than a feature attached directly to a view controller.
A reliable design separates episode data, network activity and user interface code from the audio engine. UIKit can display the library and controls, while an AVPlayer-based controller owns the current media item, playing state, position and queue. This separation makes the app easier to test and allows the same playback logic to support a compact interface, a lock screen and external speakers.
Australian listening habits shape what the app must handle. Many listeners use podcasts during Sydney train journeys, Melbourne tram rides, Brisbane bus commutes, driving or household chores, often with the screen locked. The market also includes ABC programs, independent Australian shows and established services such as ABC Listen, Pocket Casts, Spotify and Apple Podcasts. A local player therefore needs dependable background audio, useful offline episodes and clear handling of variable mobile coverage.
Content and data rules deserve attention from the beginning. The Copyright Act 1968 governs many uses of podcast material, so an app should only download or redistribute episodes with the publisher’s permission. If it collects account details or identifiable analytics, the Privacy Act 1988 and Australian Privacy Principles may apply. These concerns belong in the product architecture, not in a final compliance checklist added after launch.
Designing a playback architecture
Begin with a small episode model containing an identifier, title, show name, artwork, publication date, duration and audio URL. Keep this model separate from AVPlayerItem. The model represents information that can be stored and displayed, while the player item represents a specific piece of media ready for playback. It also allows a downloaded file to replace a remote URL without changing the interface.
Use AVPlayer rather than AVAudioPlayer for full podcast support. AVPlayer handles remote media, buffering, playback rates, timed metadata and common formats such as MP3 and M4A. A playback controller can expose a small state model to the rest of the app:
final class PlaybackController {
let player = AVPlayer()
var onProgress: ((Double) -> Void)?
private var timeObserver: Any?
func load(_ episode: Episode) {
guard let url = episode.audioURL else { return }
player.replaceCurrentItem(with: AVPlayerItem(url: url))
}
func observeProgress() {
timeObserver = player.addPeriodicTimeObserver(
forInterval: CMTime(seconds: 0.5, preferredTimescale: 600),
queue: .main
) { [weak self] time in
self?.onProgress?(time.seconds)
}
}
deinit {
if let timeObserver {
player.removeTimeObserver(timeObserver)
}
}
}
The controller should publish states such as idle, loading, playing and paused, along with the current position. Observe AVPlayerItem.status to detect a failed item, and use timeControlStatus to distinguish active playback from buffering. Store this state in one place so the interface, lock screen and command centre always report the same truth.
Turning an RSS feed into an episode library
Most podcast feeds publish RSS, so the app needs a parser that can read titles, descriptions, enclosure URLs and publication dates. XMLParser works well for a dependency-free implementation, while URLSession can retrieve the feed and artwork. Cache the parsed metadata locally so the library opens immediately and remains useful when the feed server is slow or unavailable.
Downloads require a separate path from ordinary streaming. A URLSessionDownloadTask can fetch an authorised episode into a temporary location, which can then be moved into Application Support. The episode model should record whether its source is remote or local. Playback can use the same AVPlayerItem API in either case because both are represented by a URL.
Network behaviour should reflect Australian conditions. City listeners may have stable connections, while people travelling through regional areas can move between different coverage levels. waitsForConnectivity can prevent immediate request failures, and sensible timeout and retry policies avoid leaving the interface stuck on a loading spinner. Automatic downloads should still be controlled carefully because large episodes can consume significant mobile data.
Connecting playback to a UIKit interface
A collection view or table view can present downloaded episodes, streamed episodes and recent shows. Give each row a clear title, show name, duration and download state. Diffable data sources work particularly well because feed refreshes often insert, remove or reorder rows. The selected episode should be highlighted using the playback controller’s state rather than a separate flag stored inside the cell.
The main player view needs only a few dependable controls: play or pause, elapsed time, remaining time, a scrubber, skip buttons and a menu for speed, sleep timer and download status. Update the displayed time from the periodic observer instead of running a separate timer. When the user moves the slider, convert the value into CMTime and seek the player:
@objc private func scrubberChanged(_ sender: UISlider) {
let time = CMTime(seconds: sender.value, preferredTimescale: 600)
playbackController.seek(to: time)
}
Accessibility is part of the interface, not an optional enhancement. Support Dynamic Type, VoiceOver and high-contrast display modes, and expose meaningful labels for playback controls. Episode titles vary from short broadcasts to long independent programmes, so allow them to wrap without clipping. Australian English spelling can be applied consistently to interface copy, including terms such as “organise” and “colour”.
Supporting background listening and system controls
Configure the shared audio session for spoken content before starting playback:
try AVAudioSession.sharedInstance().setCategory(.playback, mode: .spokenAudio)
try AVAudioSession.sharedInstance().setActive(true)
The playback category allows audio to continue when the screen locks, which is essential for commuters and people listening while cooking or walking. Listen for interruption and route-change notifications. An incoming call should pause playback, while a change to headphones or a car stereo may require rebuilding the active audio route. Restore playback only when the system and user experience permit it.
Add metadata and commands through MPNowPlayingInfoCenter and MPRemoteCommandCenter. The Now Playing panel should include the episode title, show name, artwork, duration and elapsed time. Play, pause, seek and skip commands can be connected to the playback controller. Remote command handlers execute outside the usual view lifecycle, so they must reach the same controller instead of relying on a button in the current screen.
Enable the Audio background mode in Xcode’s signing and capabilities settings, but use it only when the app genuinely provides continuing audio. Test with a locked device, Bluetooth headphones, a car connection and interruptions from other apps. A sleep timer can stop playback after a selected period, while automatic playback of the next episode should remain predictable and visible to the listener.
Preparing for real-world release
Test with real episode files from several Australian publishers because hosts vary in how they deliver metadata, artwork and audio. Include short and long episodes, local files, expired links, slow responses and interrupted downloads. Exercise the app on devices with limited storage and on networks that switch between Wi-Fi and cellular data. Observe memory use during long sessions and confirm that timers and observers are removed when their owning objects are released.
Release documentation should explain what the app stores, how episodes are downloaded and whether listening history is synchronised. If personal information is collected, document its purpose, access controls, retention and deletion process. A paid subscription should also have clear renewal and cancellation information, with consumer-law obligations considered alongside the App Store rules.
The lesson to remember is that a dependable podcast player is built around one source of playback truth. Feed management, downloads, UIKit presentation and background controls should cooperate without duplicating state, while network failures and interruptions are treated as normal operating conditions. With that foundation, AVFoundation can support the quiet moments, busy commutes and offline listening that shape podcast use in Australia.