3

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.

wayneh
  • 4,393
  • 9
  • 35
  • 70
  • Did you try `MPNowPlayingInfoCenter.default().setValuesForKeys(nowPlayingInfo)` instead of `MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo` ? – Arash Etemad Feb 17 '19 at 14:44
  • @ArashEtemad I tried your suggestion but it throws this error: "Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key title.'" Have you tried this yourself? Perhaps you should try my sample project? – wayneh Feb 17 '19 at 17:08
  • you are right it throws an exception, Are you forced to use `.applicationMusicPlayer` ? you can show song information by using `.systemMusicPlayer` but I think it handles showing informations automatically. – Arash Etemad Feb 18 '19 at 10:03

2 Answers2

0

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:

raphael
  • 777
  • 1
  • 9
  • 18
0

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.

Arash Etemad
  • 1,827
  • 1
  • 13
  • 29
  • I'm using the applicationMusicPlayer so that it can play in the background, and also so that it is not tightly linked to the systemplayer. If I change from application to system, I will lose some important functionality and gain some troublesome issues. – wayneh Feb 21 '19 at 12:53