3

I am trying display audio controls on the lock screen, but the problem is the audio control does not display anything in the lock screen. I already enabled background modes and the audio plays in the background.

In app delegate class, right when my app launches, I set up my audio session

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    setupAudioSession()
    UIApplication.shared.beginReceivingRemoteControlEvents()
    return true
}

func setupAudioSession(){
    do {
        try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, with: [])
        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)
    }
}

In my main controller, I call the setupLockScreen function after I play the audio.

func setupLockScreen(){
    let commandCenter = MPRemoteCommandCenter.shared()
    commandCenter.playCommand.isEnabled = true
    commandCenter.playCommand.addTarget { (event) -> MPRemoteCommandHandlerStatus in
        if self.player?.rate == 0.0 {
            self.player?.play()
            return .success
        }
        return .commandFailed
    }
    var nowPlayingInfo = [String : Any]()
    nowPlayingInfo[MPMediaItemPropertyTitle] = "My Song"
    nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = audioplayerItem.duration.seconds
    nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = audioplayerItem.asset.duration.seconds
    nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = player?.rate

    MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
}

I read many articles and I looked all the qustions in Stack Overflow but no luck.

Pang
  • 9,564
  • 146
  • 81
  • 122
Nayef
  • 33
  • 4

2 Answers2

2

Is the problem that you nowPlayingInfo is not displaying on the lock screen or is it that the controls you added are not responding?

To have your nowPlayingInfo appear on the lockscreen, you need to do a few things listed here in an answer on Handling External Player Events Notifications. One is to have a non mixable audio session (which you already have with AVAudioSessionCategoryPlayback) and the other is to be playing audio or only recently have stopped playing audio. Is your app actually playing audio?

Background audio, which you have is a sort of derived requirement, because lockscreen=>background=>background audio needed to play audio, so I should add that to the other answer.

If the problem is that the lockscreen controls aren't enabled/responsive, then try adding a pauseCommand as well. The playCommand/pauseCommand pair seem to be a minimum requirement for receiving lockscreen/external player commands.

p.s. your MPNowPlayingInfoPropertyElapsedPlaybackTime looks wrong. shouldn't it be

nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = CMTimeGetSeconds(audioplayerItem.currentTime)
Rhythmic Fistman
  • 34,352
  • 5
  • 87
  • 159
  • 1
    Thanks for this. The 'non mixable audio session' is what solved it for me. When I had this as an option for my audio session in app delegate, the controls did not appear. – Zenman C Aug 17 '19 at 17:02
0

Do it like this (this is swift 3):

override func viewDidLoad() {
    super.viewDidLoad()

    UIApplication.shared.beginReceivingRemoteControlEvents()
    let commandCenter = MPRemoteCommandCenter.shared()

    commandCenter.pauseCommand.addTarget { (event) -> MPRemoteCommandHandlerStatus in
        //Update your button here for the pause command
        return .success
    }

    commandCenter.playCommand.addTarget { (event) -> MPRemoteCommandHandlerStatus in
        //Update your button here for the play command
        return .success
    }

}

This answer found HERE

Jake
  • 2,126
  • 1
  • 10
  • 23
  • How is that different from what he's doing? Plus it just repeats the other answer's code; don't do that. A comment would have sufficed. – matt Mar 23 '18 at 02:50