How to play an audio file in iOS using Swift 2.0 and Xcode 7.0

How to play an audio file in iOS using Swift/Xcode version 7.0

I've also been fascinated by mobile applications. But I've spent most of my career coding desktop or web based applications. So I decided to change that by taking Udacity's Intro to iOS App Development with Swift. Good decision, right? Right.

For the first project, we develop a very basic application which records audio and plays it back. The first iteration requires playing a predefined audio file back at different speeds (or rates, according to AVAudioPlayer nomenclature). Below are the high level steps and code samples to play an audio file using Swift 2.0.
  1. First things first, you need an audio file, say a movie_quote.mp3 somewhere on your file system.  Locate it!
  2. Drag that file from source directory to the target directory. In this case, the target directory is the Pitch Perfect project directory in Xcode. Now the file will be made available to the application source code. Make sure you select the option "Copy items if needed" so a local copy is saved with the project.

  3. Google the AVFoundation for iOS framework. This framework enables iOS developers to play and create time-based audiovisual media. More specifically, I care about audio and the AVAudioPlayer class. Analogy: Imagine that the AVFoundation is a big gym bag full of basketballs. Each basketball represents one Swift class from the framework. In order to  access just one ball, we need to get the entire bag. "Need" in the Swift world means "import".  So we need to import the framework. 
  4. Import AVFoundation to the custom view controller
  5.  In my view's controller class, I need to import the AVFoundation class as illustrated below in order to access and use its classes.
  6. At this step, I would suggest reading up on the AVAudioPlayer class documentation, the main class of interest for the purposes of this blog post. I am interested in APIs that pertain to just audio and playing sound files.
  7. Let's get into the heart of the code now. I hope you recall the different controller lifecycle functions and events. If not, now might be a good time to look it up and review it. In my controller, I need to read in the full path name for the mp3 file and then create an audio player to playback that file. Since it needs to be executed just once when the controller first loads, I'll add the code to viewDidLoad.  The code will need to locate the current directory for the application / application executable and then get the full path name for movie_quote.mp3.  I then use the full path name to then construct an instance of the AVAudioPlayer. Also note that AVAudioPlayer should be instantiated as a class level variable. I'll need to reference the player in multiple functions so its scope must be set at the class level. Note here that the filePath is referenced as an optional when constructing AVAudioPlayer; its possible that the file path might be nil here, meaning the file was not found using NSBundle. Aside: To review the documentation for NSBundle (or any other class or function already present) in Xcode, click on the class name while simultaneously pressing down the option button. A small popup should appear with the documentation.
  8. Next I want to control how fast or slow the audio will playback. The default rate is 1.0 which is normal playback speed. In order to modify the rate property on the AVAudioPlayer instance, I need to first enableRate updates on the audioPlayer. That can be set just once in viewDidLoad. Updating the rate property is done on a case by case scenario. In my case, I need one slow playback and one fast playback. The code is almost identical; the only variable is the rate value, which by the way is of type Float. So set it to something like 1.5 or 2.0 for a faster than normal playback speed or 0.5 for a very sloooooww playback. My code below avoids code duplication by extracting common code out to one function and having the fast and slow functions utilize it. 

    That wraps up this blog post. I hope this served as a good introduction to playing audio files in iOS using Swift. Feel to post any questions, concerns or corrections. I am always learning and not anywhere close to perfect and prefer that it says that way. 

    Til we code again,
    Sonya

Comments

Popular posts from this blog

Women Techmakers New York City 2017