Apologies
You must be signed in to watch this lesson.
Handling Interruptions and Lock Screen Fixes
Podcasts
When you are building out an audio playback application, you have to be aware of the fact that your app can be interrupt at any give time. For example, you might receive a phone call while you are listening to an episode. In order to capture these events, we'll add an observer to NotificationCenter to monitor for interruptions. Next we handle how to update our UI whenever an interruption begins, and resume playback when an interruption ends.
### PlayerDetailsView.swift ```swift deinit { //Don't forget to remove yourself as an observer in deinit. //If you don't include this, your object will be forever retained. NotificationCenter.default .removeObserver(self, name: .AVAudioSessionInterruption, object: nil) } ```

Comments (3)
xuano
5 years ago
Hi Brian, I don't know if this will change in future videos, but as the PlayerDetailsView is owned by the MainTabController, I'd say that the deinit snippet that you've provided in the description section, will never be executed. And to avoid adding the observer when we run the application and we haven't selected an episode yet, I moved the setupInterruptionObserver() method call from awakeFromNib to the episode didSet observer. This way, the interruption observer will be added once the episode actually starts playing.
Brian Voong
5 years ago
xuano
5 years ago
Hey Brian, You're right. I thought of that after I posted the comment, so it's handled. Thanks ;)
Yarduza
5 years ago
Hey All, I tried modifying the code a bit, it doesn't work and I can't figure out why: Instead of accessing MPNowPlayingInfoCenter.default().nowPlayingInfo each time, I added this var: var nowPlayingInfo = [String: Any](){ didSet{ MPNowPlayingInfoCenter.default().nowPlayingInfo? = nowPlayingInfo } } and in the setup I added : nowPlayingInfo[MPMediaItemPropertyArtist] = episode.author nowPlayingInfo[MPMediaItemPropertyTitle] = episode.title it only shows the first value I insert like that, if I change the order, it saves the other, however, the didSet block is being accessed twice. what did I miss?
Yarduza
5 years ago
Issue solved. I missed 2 accesses to the var down the file.
Brandon Cao
5 years ago
Hey all, Could you guide me on how you would handle an interruption if another video was playing? Use case... I have a podcast playing, then I open a link to youtube and start playing that video? Thank you!
HELP & SUPPORT