Music info showing in Command Center but not Lock

2019-05-28 07:17发布

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

enter image description here

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.

2条回答
我命由我不由天
2楼-- · 2019-05-28 07:27

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:

查看更多
做自己的国王
3楼-- · 2019-05-28 07:30

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.

查看更多
登录 后发表回答