正在处理音乐应用程序。因为我使用了MPNowPlayingInfoCenter在锁定屏幕上显示控件。
问题:搜索栏,搜索栏的当前时间与应用播放器中的当前时间不匹配。
要解决此问题,我脑中单击了一个解决方案,以在MPNowPlayingInfoCenter中再次设置当前时间。
我使用的代码如下:
func updateInfoCenter() {
guard let item = currentItem else {return}
let title = item.contentObj.title
let currentTime = item.currentTime ??
let duration = item.meta.duration ?? 0
let trackNumber = playIndex
let trackCount = QueueManager.shared.queuedItems.count
var nowPlayingInfo : [String : AnyObject] = [
MPMediaItemPropertyPlaybackDuration : duration as AnyObject,
MPMediaItemPropertyTitle : title as AnyObject,
MPNowPlayingInfoPropertyElapsedPlaybackTime : currentTime as AnyObject,
MPNowPlayingInfoPropertyPlaybackQueueCount :trackCount as AnyObject,
MPNowPlayingInfoPropertyPlaybackQueueIndex : trackNumber as AnyObject,
MPMediaItemPropertyMediaType : MPMediaType.anyAudio.rawValue as AnyObject
]
if item.contentObj.description.isEmpty{
if let artist = item.meta.artist {
nowPlayingInfo[MPMediaItemPropertyArtist] = artist as AnyObject?
}
}else{
nowPlayingInfo[MPMediaItemPropertyArtist] = item.contentObj.description as AnyObject?
}
if let album = item.meta.album {
nowPlayingInfo[MPMediaItemPropertyAlbumTitle] = album as AnyObject?
}
if item.contentObj.image.isEmpty{
if let img = currentItem?.meta.artwork {
nowPlayingInfo[MPMediaItemPropertyArtwork] = MPMediaItemArtwork(image: img)
}else{
nowPlayingInfo[MPMediaItemPropertyArtwork] = MPMediaItemArtwork(image: AppImages.appLogo.image)
}
}else{
if let imgUrl = URL(string: item.contentObj.image) , !item.contentObj.image.isEmpty {
DispatchQueue.main.async {
UIImageView().pin_setImage(from: imgUrl) { (result) in
nowPlayingInfo[MPMediaItemPropertyArtwork] = MPMediaItemArtwork(image: result.image ?? AppImages.appLogo.image)
}
}
} else {
nowPlayingInfo[MPMediaItemPropertyArtwork] = MPMediaItemArtwork(image: AppImages.appLogo.image)
}
}
MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
}
func addActionsToControlCenter(){
self.addActionToPlayCommand()
self.addActionToPauseCommnd()
self.addActionToPreviousCommand()
self.addActionToNextCommand()
self.addActionToChangePlayBackPosition()
self.addActionToseekForwardCommand()
self.addActionToseekBackwordCommand()
}
func addActionToseekForwardCommand(){
MPRemoteCommandCenter.shared().seekForwardCommand.isEnabled = true
MPRemoteCommandCenter.shared().playCommand.addTarget(self, action: #selector(seekForward))
}
func addActionToseekBackwordCommand(){
MPRemoteCommandCenter.shared().seekBackwardCommand.isEnabled = true
MPRemoteCommandCenter.shared().playCommand.addTarget(self, action: #selector(seekBackword))
}
func addActionToPlayCommand(){
MPRemoteCommandCenter.shared().playCommand.isEnabled = true
MPRemoteCommandCenter.shared().playCommand.addTarget(self, action: #selector(playCommand))
}
func addActionToPauseCommnd(){
MPRemoteCommandCenter.shared().pauseCommand.isEnabled = true
MPRemoteCommandCenter.shared().pauseCommand.addTarget(self, action: #selector(pauseCommand))
}
func addActionToPreviousCommand(){
MPRemoteCommandCenter.shared().previousTrackCommand.isEnabled = QueueManager.shared.queuedItems[playIndex].contentObj.contentType == .song
if QueueManager.shared.queuedItems[playIndex].contentObj.contentType != .song{ return }
MPRemoteCommandCenter.shared().previousTrackCommand.addTarget(self, action: #selector(previousButtonTapped))
}
func addActionToNextCommand(){
guard let _ = QueueManager.shared.queuedItems[exist : playIndex] else { return }
MPRemoteCommandCenter.shared().nextTrackCommand.isEnabled = QueueManager.shared.queuedItems[playIndex].contentObj.contentType == .song
if QueueManager.shared.queuedItems[playIndex].contentObj.contentType != .song{ return }
MPRemoteCommandCenter.shared().nextTrackCommand.addTarget(self, action: #selector(nextButtonTapped))
}
func addActionToChangePlayBackPosition(){
guard let _ = QueueManager.shared.queuedItems[exist : playIndex] else { return }
MPRemoteCommandCenter.shared().changePlaybackPositionCommand.isEnabled = QueueManager.shared.queuedItems[playIndex].contentObj.contentType == .song
if QueueManager.shared.queuedItems[playIndex].contentObj.contentType != .song{ return }
MPRemoteCommandCenter.shared().changePlaybackPositionCommand.addTarget(self, action: #selector(changePlaybackPosition))
}
func setCurrentTimeForNowPlayingCenter(){
}
func enableNextAndPrevious(){
}
func disableNextAndPrevious(){
}
@objc func playCommand(){
guard let tabBar = sharedAppDelegate.tabBar else { return }
tabBar.audioPlayerView.playPausePlayer()
self.updateInfoCenter()
}
@objc func pauseCommand(){
guard let tabBar = sharedAppDelegate.tabBar else { return }
tabBar.audioPlayerView.playPausePlayer()
self.updateInfoCenter()
}
@objc func previousButtonTapped(){
AudioPlayerManager.shared.previousButtonTapped()
}
@objc func nextButtonTapped(){
AudioPlayerManager.shared.nextButtonTapped()
}
@objc func seekForward(){
print_debug("seekForward called")
}
@objc func seekBackword(){
print_debug("seekBackword called")
}
@objc func changePlaybackPosition(event : MPChangePlaybackPositionCommandEvent){
self.seek(toSecond: Int(event.positionTime), shouldPlay: false)
DispatchQueue.delay(0.5) {
self.updateInfoCenter()
}
}
@objc func setCanBeControlledByScrubbing(){
}