In my audio player application, on first boot, when I select an audio track inside the app and then lock my device, the audio controls do not appear on the lock screen. Only when I open the app and scrub the track, or change to another track, the controls appear. How do I get them to appear immediately upon playing a song?
I have the following code written in my view did load function.
let commandCenter = MPRemoteCommandCenter.shared()
commandCenter.nextTrackCommand.addTarget(self, action: #selector(SecondViewController.nextSong))
commandCenter.previousTrackCommand.addTarget(self, action: #selector(SecondViewController.prevSong))
commandCenter.pauseCommand.addTarget(self, action: #selector(SecondViewController.pausePlayer))
commandCenter.playCommand.addTarget(self, action: #selector(SecondViewController.playPlayer))
commandCenter.changePlaybackPositionCommand.addTarget(self, action: #selector(SecondViewController.changedThumbSliderOnLockScreen(event:)))
With the audioSession variable being defined outside of the class in the view controller.
var audioSession = AVAudioSession.sharedInstance()
Then later, in my updateCommandCenter function
let songInfo :[String : Any] = [MPMediaItemPropertyTitle :songs[currentSong], MPMediaItemPropertyArtist : "Artist Here", MPMediaItemPropertyArtwork: MPMediaItemArtwork(image: albumArtwork), MPMediaItemPropertyPlaybackDuration: audioPlayer.duration, MPNowPlayingInfoPropertyElapsedPlaybackTime: audioPlayer.currentTime]
MPNowPlayingInfoCenter.default().nowPlayingInfo = songInfo
SharingManager.sharedInstance.welcomeMessage = songs[currentSong]
I have already set my application background modes to Audio and Airplay.