Apologies
You must be signed in to watch this lesson.
Previous and Next Track Commands
Podcasts
Additional controls can be implemented in the lock screen as well, such as skipping ahead 15 seconds or skipping to the next track entirely. In today's lesson, let's take a look at how we can look through our catalog of episodes and play the next available one. My challenge to you in this lesson is to implement the previous track command. Good luck.

Comments (4)
Te-Jen Wu
6 years ago
Hi Brian, While playing any episodes then switching to another audio app and playing its audio, our app audio will be paused. However, the playPause button still remain in pause state. How can we solve this issue?
blues
6 years ago
Hi, You could observe Audio interruption notifications in awakeFromNib(): NotificationCenter.default.addObserver(self, selector: #selector(handleInterruption(_:)), name: .AVAudioSessionInterruption, object: nil) And then make function to handle those interruptions: @objc func handleInterruption(_ notification: Notification) { // Handle interruptions playPauseButton.setImage(#imageLiteral(resourceName: "play"), for: .normal) miniPlayPauseButton.setImage(#imageLiteral(resourceName: "play"), for: .normal) }
Te-Jen Wu
6 years ago
Hi Blues, Thanks for kindly replying such great solution.
MaurixFx
6 years ago
Hello Brian. I have problems with the playback time, it does not move in lock screen, if I pause the song it moves but it stops, I try using your final project on my Iphone 7 plus device and I have the same problem, please help.
st4n
5 years ago
I am having the same issue.. the problem seems to be in observeBoundaryTime function - the code in the addBoundaryTimeObserver executes only once...
Dew Douglass
5 years ago
I'm surprised no one suggested using the modulo operator. This is the picture perfect use case for what the modulo is for. @objc fileprivate func handlePreviousTrack() { changeTrack(moveForward: false) } @objc fileprivate func handleNextTrack() { changeTrack(moveForward: true) } fileprivate func changeTrack(moveForward: Bool) { let offset = moveForward ? 1 : playlistEpisodes.count - 1 if playlistEpisodes.count == 0 {return} let currentEpisodeIndex = playlistEpisodes.index { (episode) -> Bool in return self.episode.title == episode.title } guard let index = currentEpisodeIndex else {return} self.episode = playlistEpisodes[(index + offset) % playlistEpisodes.count] }
Brian Voong
5 years ago
azemii
4 years ago
Super clean! Thanks.
HELP & SUPPORT