I'm trying to implement player controls on iOS lock screen for my Remote Control application that controls Music Player on PC. So basically I need to control playback outside of iOS device. I've enabled Background Modes in capabilities (Audio) and tried to set MPRemoteCommandCenter lock screen controls, but they aren't shown. Here is code:
class RemoteCommandHandler: NSObject {
static let shared = RemoteCommandHandler()
private let player = AVPlayer()
private let audioSession = AVAudioSession.sharedInstance()
private let commandCenter = MPRemoteCommandCenter.shared()
func initRemote() {
//UIApplication.shared.beginReceivingRemoteControlEvents()
do {
if #available(iOS 10.0, *) {
try audioSession.setCategory(.playback, mode: .default, options: [])
} else {
// Fallback on earlier versions
try audioSession.setCategory(.playback)
}
} catch {
NSLog("\(error)")
}
do {
try audioSession.setActive(true)
NSLog("AVSession is active")
} catch {
NSLog("\(error)")
}
setupRemoteTransportControls()
setupNowPlaying()
}
func setupRemoteTransportControls() {
commandCenter.togglePlayPauseCommand.isEnabled = true
commandCenter.togglePlayPauseCommand.addTarget(self, action: #selector(controlPlayPause))
}
func setupNowPlaying() {
var nowPlayingInfo = [String : Any]()
nowPlayingInfo[MPMediaItemPropertyTitle] = NowPlayingInfo.getValue(.Title)
if let image = UIImage(named: "DemoArtwork") {
nowPlayingInfo[MPMediaItemPropertyArtwork] = MPMediaItemArtwork(image: image)
}
nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = TimeInterval(exactly: 10)
nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = NowPlayingInfo.getDurationMs() / 1000
nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = 1.0
MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
}
}
Is it even possible to do what I want? What I'm doing wrong?