Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MPNowPlayingInfoCenter doesn't update any information when after assigning to nowPlayingInfo

I'm making an iOS app using Swift 3 in which displaying information about the currently playing item on the lock screen and control center would be nice.

Currently, I'm using the following code to attempt to insert this information into the nowPlayingInfo dictionary. I've also included reference to the VideoInfo class that is used in videoBeganPlaying(_:).

class VideoInfo {
    var channelName: String
    var title: String
}

// ...

var videoInfoNowPlaying: VideoInfo?

// ...

@objc private func videoBeganPlaying(_ notification: NSNotification?) {
    // apparently these have to be here in order for this to work... but it doesn't
    UIApplication.shared.beginReceivingRemoteControlEvents()
    self.becomeFirstResponder()
    guard let info = self.videoInfoNowPlaying else { return }
    let artwork = MPMediaItemArtwork(boundsSize: .zero, requestHandler:
        { (_) -> UIImage in #imageLiteral(resourceName: "DefaultThumbnail") }) // this is filler
    MPNowPlayingInfoCenter.default().nowPlayingInfo = [
            MPMediaItemPropertyTitle: info.title,
            MPMediaItemPropertyArtist: info.channelName,
            MPMediaItemPropertyArtwork: artwork
        ]
    print("Current title: ", MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPMediaItemPropertyTitle])
}

The function is called when it should be, and the print statement executed, outputting Optional("title"). However, the control center and lockscreen do not update their information. Pause/play, and the skip forward button work, as I set them in viewDidLoad() using MPRemoteCommandCenter.

What's going wrong?

EDIT:

As matt pointed out, AVPlayerViewController makes the MPNowPlayingInfoCenter funky. This was my issue. I should have specified that this is the class I am using, not just AVPlayer.

See: https://developer.apple.com/reference/avkit/avplayerviewcontroller/1845194-updatesnowplayinginfocenter

like image 578
settwi Avatar asked Jan 16 '17 22:01

settwi


3 Answers

It does work, and you don't need all that glop about the first responder and so on, as you can readily prove to yourself by just going ahead and setting the now playing info and nothing else:

enter image description here

So why isn't it working for you? Probably because you are using some sort of player (such as AVPlayerViewController) that sets the now playing info itself in some way, and thus overrides your settings.

like image 92
matt Avatar answered Nov 05 '22 23:11

matt


If you are using AVPlayerViewController, you can specify that the AVPlayerViewController instance doesn't update the NowPlayingInfoCenter:

playerViewController.updatesNowPlayingInfoCenter = false
like image 5
Thomas Verbeek Avatar answered Nov 06 '22 00:11

Thomas Verbeek


This caught me out. Found a helpful post regarding this. https://nowplayingapps.com/how-to-give-more-control-to-your-users-using-mpnowplayinginfocenter/

"In order for the show the nowplayinginfo on the notification screen, you need to add this one last piece of code inside your AppDelegate’s didFinishLaunchingWithOptions function."

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
     application.beginReceivingRemoteControlEvents()
}
like image 2
Adam Avatar answered Nov 05 '22 22:11

Adam