Chapter 10
Audio Recording and Playback

The iOS SDK provides various frameworks to let you work with sounds in your app. One of the frameworks that you can use to play and record audio files is the AV Foundation framework. In this chapter, I will walk you through the basics of the framework and show you how to manage audio playback and recording.

The AV Foundation provides essential APIs for developers to deal with audio on iOS. In this demo, we mainly use these two classes of the framework:

  • AVAudioPlayer – think of it as an audio player for playing sound files. By using the player, you can play sounds of any duration and in any audio formats available in iOS.
  • AVAudioRecorder – an audio recorder for recording audio.

A Simple Demo App

To understand how to use the API, we will build a simple audio app that allows users to record and play audio. Our primary focus is to demonstrate the AVFoundation framework so the app's user interface will be very simple.

First, create an app using the Single View Application template and name it RecordPro (or any name you like). You can design a user interface like figure 10.1 on your own. However, to free you from setting up the user interface and custom classes, you can download the project template from I've created the storyboard and custom classes for you. The user interface is very simple with three buttons: record, stop and play. It also has a timer to show the elapsed time during recording. The buttons have been connected to the corresponding action method in the RecordProController class, which is a subclass of UIViewController.

Figure 10.1. RecordPro Project
Figure 10.1. RecordPro Project

Before we move onto the implementation, let me give you a better idea of how the demo app works:

  • When the user taps the Record button, the app starts the timer and begins to record the audio. The Record button is then replaced by a Pause button. If the user taps the Pause button, the app will pause the recording until the user taps the button again. In terms of coding, it invokes the record action method.
  • When the user taps the Stop button, the app stops the recording. I have already connected the button with the stop action method in RecordProController.
  • To play the recording, the user can tap the Play button, which is associated with the play method.

To continue reading and access the full version of the book, please get the full copy here. You will also be able to access the full source code of the project.

results matching ""

    No results matching ""