Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Audio playback lock screen controls not displaying on iPhone

I am testing this using iOS 10.2 on my actual iPhone 6s device.

I am playing streamed audio and am able to play/pause audio, skip tracks, etc. I also have enabled background modes and the audio plays in the background and continues through a playlist properly. The only issue I am having is getting the lock screen controls to show up. Nothing displays at all...

In viewDidLoad() of my MainViewController, right when my app launches, I call this...

 func setupAudioSession(){

   UIApplication.shared.beginReceivingRemoteControlEvents()


   do {
       try    AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, with: AVAudioSessionCategoryOptions.mixWithOthers)

       self.becomeFirstResponder()

       do {
           try AVAudioSession.sharedInstance().setActive(true)
                   print("AVAudioSession is Active")
       } catch let error as NSError {
                   print(error.localizedDescription)

       }
   } catch let error as NSError {
               print(error.localizedDescription)
   }
}

and then in my AudioPlayer class after I begin playing audio I call ...

func setupLockScreen(){
       let commandCenter = MPRemoteCommandCenter.shared()
       commandCenter.nextTrackCommand.isEnabled = true
       commandCenter.nextTrackCommand.addTarget(self, action:#selector(skipTrack))
       MPNowPlayingInfoCenter.default().nowPlayingInfo = [MPMediaItemPropertyTitle: "TESTING"]
}

When I lock my iPhone and then tap the power button again to go to the lock screen, the audio controls are not displayed at all. It is as if no audio is playing, I just see my normal background photo. Also no controls are displayed in the control panel (swiping up on home screen and then swiping left to where the music controls should be).

Is the issue because I am not using AVAudioPlayer or AVPlayer? But then how does, for example, Spotify get the lock screen controls to display using their own custom audio player? Thanks for any advice / help

like image 358
MikeG Avatar asked Mar 07 '17 19:03

MikeG


1 Answers

The issue turned out to be this line...

try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, with: AVAudioSessionCategoryOptions.duckOthers)

Once I changed it to

try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, with: [])

everything worked fine. So it seems that passing in any argument for AVAudioSessionCategoryPlaybackOptions causes the lock screen controls to not display. I also tried passing in .mixWithOthers an that too caused the lock screen controls to not be displayed

like image 165
MikeG Avatar answered Oct 13 '22 21:10

MikeG