Home Audio Controls Not Showing Up On Lockscreen
Reply: 0

Audio Controls Not Showing Up On Lockscreen

user2101
1#
user2101 Published in May 23, 2018, 6:54 pm

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.

    try! self.audioSession.setCategory(AVAudioSessionCategoryPlayback)
    try! self.audioSession.setActive(true)
    UIApplication.shared.beginReceivingRemoteControlEvents()
    self.becomeFirstResponder()

    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:)))
    updateCommandCenter()

With the audioSession variable being defined outside of the class in the view controller.

var audioSession = AVAudioSession.sharedInstance()

Then later, in my updateCommandCenter function

func updateCommandCenter(){
    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.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.480999 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO