How To Play Sounds Files And Manage Duration Progress – AVAudioPlayer Tutorial

AVAudioPlayer class is one of the AVFoundation framework classes and APIs. It comes in handy to play any type of audio file in your iOS app. The class allows to, among other, manage playback operations such as play, pause, rewind and fast forward.

It also provides a protocol that will inform your app of any interruption during playback and whenever the sound has finished playing.

In this tutorial, you will play around with some sound files using the AVAudioPlayer class. You gonna see and learn how easy it is to implement playback operations and how to report playback progress within a progress view.

As usual, let’s do it!

I prepared a starter project that we will work on, start by downloading it here 🙂

Open the project with Xcode and take a look at the Main.storyboard file. The app mainly consists on two screens. The first interface will show some audio tracks in a table view while the next one will handle all audio playback operations. So all of the AVAudioPlayer work will be implemented on that interface, you got that right 😉

Let’s start by showing up some audio files in the table view, a list from which the user can select which track to play.

First, download the sound files here, unzip the folder and drag all of the seven mp3 files to the Xcode project. Make sure Copy items if needed option is checked and click the “Finish” button.

Import mp3 files into Xcode

Next, select the ViewController.swift file from the Project navigator view and implement the following code inside:

The first two methods in the code above will populate seven rows to the table view relevant to the audio files you imported previously to the project. The tableView:didSelectRowAtIndexPath: delegate method will get the selected row and store it to a reference variable called track, it will then call a segue with the “MoveToAudioPlayer” identifier to move to the next screen.

Finally, the prepareForSegue method will cast the destinationViewController of the segue object and assign the selected track to its trackID property.

Before moving on to implement the main features for this tutorial, copy the following declaration right after the ViewController class name to add a reference for the selected track.

Next, select the Main.storyboard file, then click on the segue and set its Identifier property to “MoveToAudioPlayer” from the Attributes inspector view.

Set an Identifier to the storyboard segue

Always in storyboard, select the prototype cell in the table view and set its Identifier property to “CellID” from the Attributes inspector.

Cool, let’s move to the main part now!

Select the AudioPlayerVC.swift file from the Project navigator view and declare the trackID property to shut down the compiling error. Place the following declaration just after the class name:

You can now run the app to make sure the table view is loading correctly.

So far so good, let’s declare a reference for the audio player that will be used within different methods in the AudioPlayerVC class. Add the following property declaration to the class:

The AVAudioPlayer class belongs to the AVFoundation framework, copy the following import and place it right above the class name:

Next, locate the viewDidLoad method and implement the code below inside (after the super.viewDidLoad call):

The code above will first update the label with the track name.

Let’s breakdown the rest of it:

// 1: Here you just got the trackID value you passed from the previous view controller to append to the mp3 file path. Then you constructed a url path for the sound file.

// 2: AVAudioPlayer will try to extract the mp3 file at the specified path. If it succeeds, the audio player object will start playing the sound file, otherwise, it will jump to the catch block to handle any occurred error.

// 3: Here you used the scheduledTimerWithTimeInterval API to trigger a method every one second. That method will be responsible of updating the progress view while the sound file is playing. You also initialise the progress property of the progressView to 0 (since the current time of the sound file is 0 at the beginning of the playback).

Now let’s implement the selector that will be triggered every one second in order to update the progress view’s progress.

Implement the following code before the class closing bracket:

Each time called, the method above will update the progress as long as the sound file is playing. So in a pause state, we are now sure that the progress view will be frozen 🙂

Note: Remember the best way to know how far is the sound file, is to divide the current time by the duration of the track.

To the nice part now!

Locate the fastForward action method and place the following code inside:

Fast forwarding is just updating the current time property of the audio player. Here, you just get the current time and increment it by 5 seconds. Unless the new current time of the player exceeds the total duration of the track (in such case you call the stop function), the method will update the player current time to reflect the new duration.

Locate the fastBackward action method and implement the following code inside:

The only difference here is just you decrement the current time instead of incrementing it. This is how rewind works.

Let’s implement the remaining action methods. To do so, make the following changes:

Before running your app, make sure to stop playing the sound file once you select the back button on the navigation bar. Otherwise, the sound will remain playing and can interfere with other sounds (at such case, you will listen to a tuneless song).

To solve this, you just need to stop the audio player whenever the screen is about to dismiss. To do so, override the viewWillDisappear method with the following implementation:

That’s it folks, run your app and enjoy your audio player 🙂

As usual, you can download the final project here.

Feel free to add your comments below, I always would love to hear your thoughts!

About Malek

Malek is a passionate iOS Engineer and Founder of Medigarage Studios, a small mobile games startup. I started my iOS adventures in 2011 and since then I fell in love with it. You can hire me for your project, get in touch by Email to discuss further details. Also, feel free to reach out on Twitter and Google+.

  • timbojill

    Can this be used for short sounds ? I plan on adding a piano keyboard to one of my apps.

  • Malek_T

    Hi timbojill 🙂
    Of course you can use AVAudioPlayer to play short sounds.
    Let me know how it goes with your Piano app. I am looking forward to see it on the App Store.

  • Kevin

    Nice. Can I do the same with videos? manage duration, I want to detect when video has been finished

  • Malek_T

    Hi Kevin 🙂
    Sure! For video playback, the AVPlayerViewController class provides all what you need. Let me know if you need help.
    Note: MPMoviePlayerController use to be the class to handle movies playback, but it’s deprecated in iOS 9.

  • luca

    Hi Malek_T! Thank you for sharing your project. I a noob and I am trying to develop a tableview app including audio and images as a gallery per each audio files. I do not know how could I implement this feature in your project, so that during the playback of each audio file, the user could slide 16 images. I am trying to develop a music education app, with examples of music sung and notation. What could you suggest me to do? Thank you in advance!