Music info showing in Command Center but not Lock Screen

落花浮王杯 提交于 2019-12-22 09:31:31

问题


Using Swift 4+, iOS 11+, Xcode 10+

I've built a music player using MPMediaPlayer and I can interact with it from the Command Center, however I would like to be able to also see it on the Lock Screen.

To be honest, I'm a bit confused as to why it's showing/working in the Command Center as I have not written any code to do this. Nevertheless, I would also like it to show in the Lock Screen.

This is what I have done so far:

1) I'm using the applicationMusicPlayer and made certain something is playing during my tests:

MPMusicPlayerController.applicationMusicPlayer

2) Set the BackgroundModes to include Audio, Fetch, and Remote Notifications

3) Added AVAudioSession code (which doesn't seem to do anything as I have tried it and tried commenting it out and seen no difference):

  let session = AVAudioSession.sharedInstance()
    do {
        // Configure the audio session for playback
        try session.setCategory(AVAudioSessionCategoryPlayback,
                                mode: AVAudioSessionModeDefault,
                                options: [])
        try session.setActive(true)
    } catch let error as NSError {
        print("Failed to set the audio session category and mode: \(error.localizedDescription)")
    }

4) Used this basic code to see if I can get it to show on the lock screen with just some basic hard-coded content:

let nowPlayingInfo: [String: Any] = [
    MPMediaItemPropertyArtist: "Pink Floyd",
    MPMediaItemPropertyTitle: "Wish You Were Here",
    //MPMediaItemPropertyArtwork: mediaArtwork,
]
MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo

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

5) I know I have not implemented anything to actively update the info or respond to any commands as I'm just trying to get something to show on the lock screen at this point.

Why is the now playing info showing in the Command Center if I have done nothing to put it there?

What do I need to do to get the info to show on the Lock Screen like it does in the Command Center?

EDIT: Link to simple project that has same issue on GitLab:https://gitlab.com/whoit/lockscreentest

EDIT: I have submitted this as a bug to Apple, however they have yet to confirm or resolve this.


回答1:


I had to fill (even with empty string) at least 4 keys to see something correct on the lock screen:

  • MPMediaItemPropertyTitle
  • MPMediaItemPropertyArtist
  • MPMediaItemPropertyAlbumTitle
  • MPNowPlayingInfoPropertyPlaybackRate

you can check this NowPlayingSource code source:




回答2:


Using .systemMusicPlayer instead of .applicationMusicPlayer will solve your problem.

As apple document:

The music player does not affect the Music app’s state. When your app moves to the background, the music player stops playing the current media.

And I think it's related to not showing in locked screen.

and also systemMusicPlayer handles all song informations to show automatically.



来源:https://stackoverflow.com/questions/54171258/music-info-showing-in-command-center-but-not-lock-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!