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
.
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:
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.