Swift Audio: Record, Store, and Play

Mazen Kilani
Swift2Go
Published in
3 min readJun 19, 2021

--

Users Can Record Audio Files, Store in Core Data, and Play from a List.

AppsGym ToDo Audio Record, Stop, and Play

Scenario

Our ToDo app allows users to record audio sound files, store them in the device File Manager, as well as Core Data linked to a ToDo Note, and play them as needed from a ToDo Sounds list.

Technique

We shall utilise the AVFoundation framework, AVAudioRecorder, and AVAudioPlayer to record and play the sound files. Then use the File Manager to store the audio sound file URLs and keep records in Core Data linked to the ToDo Note of all related sound files.

We shall design 3 UIButtons on the Recording view: Record, Stop, and Play (the last recorded audio), and change their images to reflect their status (blue if active, grey is inactive). Also, we shall enter a Title for the audio recording in a UITextField, show the Recording Status in a UILabel, and save using a Save UIButton.

GIF AppsGym ToDo Audio Recording Sounds View

Audience

The article is for Swift developers who seek complete, proven, code-centric solutions to speed up their development projects. The code snippets below can be used with minimal customisation.

App Model

We based the article on the AppsGym ToDo model app, published on Apple’s App Store (as 8ToDo), and you can download the complete Xcode project on AppsGym.com, for free.

User Interfaces

We need Record, Stop, and Play UIButtons in the Audio (Sounds) recording section, a Save UIButton, a Title UITextField, and a recording Status UILabel.

Sounds View Controller Recording Layout
AppsGym ToDo Sounds Storyboard

Logic

SoundsViewController.swift will contain all the components to record, stop, play, and title the audio recording, essentially 3 UIButtons (Record, Stop, and Play), a Title UITextField, and a Save UIButton. SoundsViewController will act as the Delegate to both AVAudioRecorder and AVAudioPlayer, so it can implement their record and play functions.

We start by initialising all the components, and creating instances of AVAudioRecorder and AVAudioPlayer. Then we construct the audio file name, and define its unique URL directory in the device File Manager. We then set up the recording and playing settings for our current recording session.

We link each button (Record, Stop, and Play) with its Button Action, and change the recording status label and button image (blue if active, grey if inactive) to reflect current recording status (e.g., Ready to Record, Recording, Recording Completed, Playing).

Furthermore, we utilise the Delegate functions, audioRecorderDidFinishRecording and audioPlayerDidFinishPlaying to define the actions at the appropriate time. Then implement the Save button action to save to the device directory and to Core Data.

We implement helper functions to:

check microphone authorisation and access,

control the keyboard, and

highlight Save button only if the user enters a Title.

Code

SoundsViewController.swift Initialise

SoundsViewController.swift viewDidLoad()

SoundsViewController.swift recordButtonAction(..)

SoundsViewController.swift stopButtonAction(..)

SoundsViewController.swift playButtonAction(..)

SoundsViewController.swift audioRecorderDidFinishRecording(..)

SoundsViewController.swift audioPlayerDidFinishPlaying(..)

SoundsViewController.swift soundSaveButtonAction(..)

SoundsViewController.swift Keyboard Control

SoundsViewController.swift checkMicrophoneAccess()

SoundsViewController.swift Save Check

SoundsViewController.swift Helpers(..)

The article covered the complete setup, logic, and code to record, store, and play an audio file. We utilised AVFoundation, AVAudioRecorder, and AVAudioPlayer to record and play, and FileManager to store the audio file URL in the device. Then we linked the audio file URL to the specific ToDo Note in Core Data. Hope you find the article useful for your app. Thanks for reading!

--

--

Mazen Kilani
Swift2Go

I published 47 Swift iOS apps/games and 2 Flutter Android apps. I share complete Xcode projects, free (no ads), at AppsGym.com.