I have a Audio player that can play from the iOS command center and lock screen. When I toggle a play/pause button in my app, it should update the play/pause button in the command center (MPRemoteCommandCenter) by updating the nowPlayingInfo (MPNowPlayingInfoCenter). But it's not updating.
But when i try to control from lockscreen Play/Pause its updating in my app toggle buttons this works fine.But similarly when i am controlling from inside my app play/pause button. its not updating play/pause button in lockscreen.Always its show pause button.
Iam setting Playbackrate 1.0 for play and 0.0 for pause.But still its not updating..
Can some please give me any suggestions or anything missing in this below code.
func setupLockScreenDisplay() {
var nowPlayingInfo = [String: Any]()
nowPlayingInfo[MPMediaItemPropertyTitle] = self.currentSongName
nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = self.currentAlbum == nil ? self.totalDurationTime : playerItem.asset.duration.seconds
nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = isPlaying ? 1.0 : 0.0
nowPlayingInfo[MPNowPlayingInfoPropertyMediaType] = NSNumber(value: MPNowPlayingInfoMediaType.audio.rawValue)
nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = self.currentAlbum == nil ? self.storyPlayer.currentTime : CMTimeGetSeconds(player.currentTime())
// Set the metadata
MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
MPNowPlayingInfoCenter.default().playbackState = .playing
}
below code for remote command below code is working fine..i can able to control from lockscreen
func setupRemoteCommandCenter() {
let commandCenter = MPRemoteCommandCenter.shared()
commandCenter.playCommand.isEnabled = true
commandCenter.playCommand.addTarget { event in
self.play()
return .success
}
commandCenter.pauseCommand.isEnabled = true
commandCenter.pauseCommand.addTarget { event in
self.pause()
return .success
}
commandCenter.nextTrackCommand.isEnabled = true
commandCenter.nextTrackCommand.addTarget { event in
self.nextPlay()
return .success
}
commandCenter.previousTrackCommand.isEnabled = true
commandCenter.previousTrackCommand.addTarget { event in
self.lastPlay()
return .success
}
commandCenter.togglePlayPauseCommand.isEnabled = true
}
Here code for inside play button
func play() {
if self.currentAlbum == nil {
self.storyPlayer.play()
} else {
self.player.play()
}
self.isPlaying = true
NotificationCenter.default.post(name: Notification.Name(rawValue: kPlayerManagerChangePlayingStateRsp), object: nil)
setupLockScreenDisplay()
setupRemoteCommandCenter()
MPNowPlayingInfoCenter.default().nowPlayingInfo![MPNowPlayingInfoPropertyPlaybackRate] = 1
MPNowPlayingInfoCenter.default().nowPlayingInfo![MPNowPlayingInfoPropertyElapsedPlaybackTime] = CMTimeGetSeconds(player.currentTime())
}
code inside pause button
func pause() {
if self.currentAlbum == nil {
self.storyPlayer.pause()
} else {
self.player.pause()
}
self.isPlaying = false
NotificationCenter.default.post(name: Notification.Name(rawValue: kPlayerManagerChangePlayingStateRsp), object: nil)
setupLockScreenDisplay()
setupRemoteCommandCenter()
MPNowPlayingInfoCenter.default().nowPlayingInfo![MPNowPlayingInfoPropertyPlaybackRate] = 0
MPNowPlayingInfoCenter.default().nowPlayingInfo![MPNowPlayingInfoPropertyElapsedPlaybackTime] = CMTimeGetSeconds(player.currentTime())
}
add this code in your view did load func or app delegate
do {
try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [.mixWithOthers, .allowAirPlay])
print("Playback OK")
try AVAudioSession.sharedInstance().setActive(true)
print("Session is Active")
}
catch {
print(error)
}
and on your background modes in capabilities.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.