How to play an MP3 File in Swift

The basic Xcode firmware contains the AVFoundation module, but the constraint to using it is you will only able to play sound with “.wav” extension and is not able to play MP3 sound. In this tutorial, we discuss the main and easiest methods of playing an MP3 File in Swift

Making the Storyboard

To see the frameworks responsible for audio is Swift, we will need to create an Xcode Project File. To do this, simply open Xcode and select new project, or use the shortcut Cmd + N. After you will need to edit the default Main.Storyboard file, and customize it for your needs. For this tutorial, we will be using a simple label and button as shown:

How to play an MP3 File in Swift

  • To add elements like buttons, labels etc, we can browse through the library (Indicated by the “+” button) on our storyboard page. To add the elements to our canvas, simply drag and drop the elements from the library.
  • For Action elements, that require input from the user like buttons, checkboxes, or textfields, we need to create an Action function that runs every time a button is clicked, held or released. For our button, we can simply create this function by opening the Assistant, shortcut: Control + Option + Command + Enter, and while holding the Control key drag the button on the code editor. Name this function “playOrPause”, as it will help with playing or pausing the sound later on.
  • There are several actions that can trigger a button; such as releasing the button, holding a button, or the most common clicking a button. For this reason, the most common IBAction is the “Touch up Inside” which acts like a normal button triggered when pressed and released. briefly.

play mp3 in Swift

Play an MP3 File using AVAudioPlayer in Swift

To play any sound in Swift, we need the AVAudioPlayer frameworks. The AVAudioPlayer is an object that allows you to play audio through a file or buffer. Although this object traditionally uses files of type “.wav”, we can use the in-built methods to modify the function to play an MP3 file. This is how you can use it to play an mp3 file:


import AVFoundation  

var player: AVAudioPlayer!  

func playMp3File() {
    let url = Bundle.main.url(forResource: "happybirthday", withExtension: "mp3")
    do {
        player = try! AVAudioPlayer(contentsOf: url) 
    } catch {
  • You must import the audio file in Xcode, or else it won’t work. This can be done by creating a folder in the package named as “Sounds” or any name of choice.
  • player” is now an AVAudioPlayer object. References will be made to “player“, or any variable name.
  • Bundle.main.path is a method to locate the file in Xcode.  Here happybirthday is the name of the file, whereas the ofType parameter is the type of mile (mp3, wav, etc.)
  • try! AVAudioPlayer(contentsOf: url) transfers the player object. Here, try! is important because it catches the error if no such file exists.
  • To play the file, the function used is .play(), and so to access the Audioplayer object and play the content we use player?.play(). Note: the question mark is used to first make sure that input is not null, avoiding errors when testing.

Pausing and Resuming sound using AVAudioPlayer

There are several methods that we can implement using the AVPlayerFoundation object, like the play/pause functionality common in almost all audio media apps. This can be done using AVPlayerFoundation with the help of the storyboard that we created. The storyboard will allow actual input processing from the user and in our case, it will be done so by using the button we created. We can program out IBAction function as:

@IBAction func playOrPause(_ sender: UIButton) {
        if player != nil {
            player = nil
        } else {
  • As you can see, this action method is quite simple. To avoid errors with our AV Audio Player, our function first ensures that the player is not null, avoiding errors with the sound and crashing the app.
  • The logic is simple, the first if condition is that if the audio is initially playing, meaning the play is not nil, then stop the player from playing music and make it nil. The else statement ensures that the second time the button in clicked it resets the player to not nil, thus resuming the music.

Confused about logic and if statements? If NOT Conditional case in Swift

Leave a Reply

Your email address will not be published. Required fields are marked *