xcode - MPNowPlayingInfoCenter info is not displayed on iOS 8
The problem is that you are not satisfying the requirements to become master of the lock screen and control center, as I explain in my book. You should be seeing the modern (iOS 8) equivalent of this:
The fact that you are not seeing it suggests that you are omitting one or more of the requirements, which I list (quoting directly from my book here):
- Your app must contain a UIResponder in its responder chain that
returns YES fromcanBecomeFirstResponder
, and that responder must
actually be first responder. - Some UIResponder in the responder chain,
at or above the first responder, must implementremoteControlReceivedWithEvent:
. - Your app must call the UIApplication
instance methodbeginReceivingRemoteControlEvents
. - Your app’s audio
session’s policy must be Playback. - Your app must be emitting some sound.
I don't know which of those you are omitting; it could be more than one. You might like to compare your code with a working example, here:
https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/iOS7bookExamples/bk2ch14p653backgroundPlayerAndInterrupter/backgroundPlayer/
backgroundPlayer/ViewController.m
iOS MPNowPlayingInfoCenter not Displaying Using AVPlayer and AVAudioSession
Okay, I solved it. My app was fine; the issue was that the simulator wasn't acting like an actual device. When I tested it on an actual iPad, the lock screen info displayed perfectly. It still doesn't display on the simulator, though. Thank you for your time.
MPNowPlayingInfoCenter is not showing but background playing does work
I solved it by adding
func setupNowPlayingInfoCenter(){
UIApplication.shared.beginReceivingRemoteControlEvents()
MPRemoteCommandCenter.shared().playCommand.addTarget {event in
self.play()
return .success
}
MPRemoteCommandCenter.shared().pauseCommand.addTarget {event in
self.pause()
return .success
}
MPRemoteCommandCenter.shared().nextTrackCommand.addTarget {event in
self.goForward()
return .success
}
MPRemoteCommandCenter.shared().previousTrackCommand.addTarget {event in
self.goBackward()
return .success
}
}
MPNowPlayingInfoCenter defaultCenter will not update or retrieve information
I finally figured out the problem, I was not prompting my app to receive remote control events, simply adding this line fixed the problem:
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
How to Set Artwork Image for MPNowPlayingInfoCenter
Just in case anyone finds this question and was going crazy like myself, I was able to set the image by correctly resizing my UIImage
to the incoming size
parameter given to the MPMediaItemArtwork
callback function. As it says in the docs:
The request handler returns an image in the newly requested size.
The requested size must be less than the boundsSize parameter.
Then setting my image
to the incoming size
parameter with the following extension, the MPMediaItemPropertyArtwork
was finally successfully set.
extension UIImage {
func imageWith(newSize: CGSize) -> UIImage {
let renderer = UIGraphicsImageRenderer(size: newSize)
let image = renderer.image { _ in
self.draw(in: CGRect.init(origin: CGPoint.zero, size: newSize))
}
return image.withRenderingMode(self.renderingMode)
}
}
if let image = UIImage(named: "image_here") {
nowPlayingInfo[MPMediaItemPropertyArtwork] =
MPMediaItemArtwork(boundsSize: image.size) { size in
// Extension used here to return newly sized image
return image.imageWith(newSize: size)
}
}
MPNowPlayingInfoCenter doesn't update any information when after assigning to nowPlayingInfo
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.
Related Topics
Refresh Devices in Team Provisioning Profile Managed by Xcode 7
Swift - How to Hide Back Button in Navigation Item
How Does Cellforrowatindexpath Work
Enterprise In-House App Distribution
How to Show Uialertcontroller from Appdelegate
Custom Interactive Transition Animation
How to Detect the Orientation of the Device on iOS
How to Change the Tint Color of the Clear Button on a Uitextfield
Why Does My Programmatically Created Screenshot Look So Bad on iOS 7
Uitableviewcell Checkmark to Be Toggled on and Off When Tapped
Restore Already Bought In-App-Purchases on Iphone
Add Lefthand Margin to Uitextfield
Crashlytics in iOS Won't Proceed Past "Build Your Project" in Fabric App
How to Add an Action to a Button Programmatically in Xcode
Cannot Subscript a Value of Type '[Nsobject:Anyobject]' with an Index of Type 'String'
Exc_Bad_Access Error When Trying to Change Bool Property
iOS 7 Uiwebview Keyboard Issue
The Operation Couldn't Be Completed. (Com.Facebook.Sdk Error 2.) iOS6