Apologies
You must be signed in to watch this lesson.
Background Audio Mode + Command Center Controls
Podcasts
Whenever your application goes into the background, all activity is stopped by the operating system. This is to prevent your application from taking up valuable resources that are needed by other applications. However Apple makes an exception for applications that want to play audio in the background. Let's go ahead an enable this feature in the settings of our application. Now that our podcast is playing in the background, it would be nice to have some way to control the playback, such as pausing or skipping ahead in the timeline. This is done by setting up some actions on MPRemoteCommandCenter.

Comments (6)
blues
6 years ago
Great videos, I added following notification observer to detect when headphones are removed and podcast stops playing. To awakeFromNib() I added: NotificationCenter.default.addObserver(self, selector: #selector(audioRouteChanged), name: .AVAudioSessionRouteChange, object: nil) Then I added function to handle the audio route change: @objc func audioRouteChanged(note: Notification) { if let userInfo = note.userInfo { if let reason = userInfo[AVAudioSessionRouteChangeReasonKey] as? Int { if reason == AVAudioSessionRouteChangeReason.oldDeviceUnavailable.hashValue { // headphones plugged out -> continue playback or change the play/pause icon //player.play() playPauseButton.setImage(#imageLiteral(resourceName: "play"), for: .normal) miniPlayPauseButton.setImage(#imageLiteral(resourceName: "play"), for: .normal) } } } } And lastly I removed the observer: deinit { NotificationCenter.default.removeObserver(self) }
rufatmirza
6 years ago
That's useful, thanks.
leo.geneva@gmail.com
5 years ago
Cool beans, thanks Blues.
Aurélien Haie
5 years ago
Hi Brian, I still not enrolled for a Apple developer account (I will do it soon) and so I run the app on the simulator. Is it normal I don't see the control center on it? When i drag down the control center i only see a blurred background with no control button..
Brian Voong
5 years ago
Aurélien Haie
5 years ago
Ok, thanks!
辛忠翰
5 years ago
Hi Brian, ur class is really helpful for me, thanks for ur fabulous class. The app is running successfully on the simulator, however, I faced a problem when trying to run the application on my IPhone8 plus. I got an error log when the app tried to parse the mp3 website. The error code is down below. ------------------------------------------------------ podUrl: https://feeds.soundcloud.com/stream/393187617-brian-hong-voong-storyboard-vs-code-speedrun.mp3 2018-04-16 11:30:27.261705-0600 Podcasts[8633:4436884] CredStore - performQuery - Error copying matching creds. Error=-25300, query={ class = inet; "m_Limit" = "m_LimitAll"; "r_Attributes" = 1; sync = syna; } --------------------------------------- It can grab the pod information, but it can't play .mp3 anymore. I also found the discussion from the stackOverFlow, https://stackoverflow.com/questions/46099940/credstore-perform-query-error?answertab=active#tab-top Plz help solve this problem, thanks lot. p.s. The link down below is my code, plz check it if u need it, thanks^^ https://github.com/HsinChungHan/Podcasts.git
Dew Douglass
5 years ago
Thank you sir.
awjenson
5 years ago
Why does the code require .shared() for several of the code that we write? E.g. setupAudioSession and setupRemoteControl both have it in their blocks.
dita
4 years ago
I am getting this error after enabling the capability for background audio: "Can't end BackgroundTask: no background task exists with identifier 1 (0x1), or it may have already been ended. Break in UIApplicationEndBackgroundTaskError() to debug." Does anybody have a solution for this?
dita
4 years ago
This problem goes away when setting the AVAudioSession.
HELP & SUPPORT