Playing MP3 Files With Swift
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!
Also, if you need to pause audio playback, call this function on audioPlayer.
Thanks for reading! Make sure to follow me for more helpful articles, and clap if you found this helpful!