Mr. SwiftUI

Yello! Today we’re going to learn how to play MP3 files with Swift. Let’s get started!

We’ll need Foundation & AVFoundation to get started, so import those, and let’s make a new class, MusicPlayer.

Now, let’s create a new function to play our MP3 files, playFile.

Next, let’s access our MP3 file.

Now, let’s create an AVAudioPlayer. We’ll also have to make MusicPlayer an ObservableObject.

Then we’ll load the audioPlayer with our MP3 file and then play it.

That’s it! Now all you have to do is run these lines of code to play your MP3 file!