Apologies
You must be signed in to watch this lesson.
Play Downloaded Audio Files Offline
Podcasts
With our application now downloading files locally onto our devices, its time to now implement some code to actually play our podcast episodes offline. This can become a bit tricky if you've never used the file system before for iOS applications. Essentially every time you launch your application, the operating system creates a new container that holds all of your files. This container changes each time your restart your app. Therefore, in order to retrieve the correct file path, you'll have to go through FileManager.default. I'll show you the necessary code for this in today's lesson. Enjoy.

Comments (6)
Tube
6 years ago
Why haven't we been using guid of RSSFeedItem instead of episode.title? BTW, I couldn't figure how to access the guid which is supposedly a String....
Huo En Ci
5 years ago
Don't quite understand your question mate?
Huo En Ci
5 years ago
I will refactor it a little differently: private func playEpisode() { var trackUrl: URL if let fileUrl = self.episode.fileUrl { guard var trueLocation = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { return } guard let fileURL = URL(string: fileUrl) else { return } let fileName = fileURL.lastPathComponent trueLocation.appendPathComponent(fileName) trackUrl = trueLocation print("from file") } else { print("from stream") guard let streamUrl = episode.streamUrl, let url = URL(string: streamUrl) else { return } trackUrl = url } let playerItem = AVPlayerItem(url: trackUrl) player.replaceCurrentItem(with: playerItem) player.play() playPauseButton.setImage(#imageLiteral(resourceName: "pause"), for: .normal) }
tobitech
5 years ago
Hello Brian how can one delete the downloaded file from the Documents folder on the user's folder when you delete the episode from the DownloadsController and from the list of saved downloads from user defaults?
Brian Voong
5 years ago
MaurixFx
5 years ago
How implement multiple downloads like a Spotify?
inboxforapps@gmail.com
5 years ago
Brain, happy new year! I'm having trouble playing the offline episodes in the background (using Remote Control). It works fine for the first offline episode but when I move on the second, play/pause plays the previous offline episode in the background. So, it will be two or more files playing at the same time. What's the potential workaround?
Ally Makongo
4 years ago
Hi Brian Am getting this error on ios13 *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Unsupported action method signature. Must return MPRemoteCommandHandlerStatus or take a completion handler as the second argument.' Whats the solution for this?
Ally Makongo
4 years ago
any idea?
red123
4 years ago
hello, I am facing the same issue. any help ?
Brian Voong
4 years ago
red123
4 years ago
got it! it is working Thanks
HELP & SUPPORT