Я создаю музыкальное приложение, которое может интегрироваться с CarPlay, все в порядке... Я могу отображать имя исполнителя, название, название альбома и какой-то контроллер кнопок на музыкальном проигрывателе. Однако существует проблема, заключающаяся в том, что при воспроизведении любой песни из CarPlay кнопка в CarPlay не обновляется так же, как кнопка на устройстве iOS, и время поиска не перемещается.
Что я должен делать ? Я искал много ресурсов, чтобы найти решение, но все же.
Примечание. Я тестирую его только на симуляторе (не на реальном устройстве).
Это шоу PlayingInfor:
private func nowPlayingInfoOverwrite(time: CMTime) {
if let nowPlayingItem: PlaylistItem = self.nowPlayingItem {
let info: NSMutableDictionary = NSMutableDictionary()
info[MPMediaItemPropertyArtist] = nowPlayingItem.mediaItem?.artist?.name
info[MPMediaItemPropertyAlbumTitle] = nowPlayingItem.mediaItem?.album?.title
info[MPMediaItemPropertyTitle] = nowPlayingItem.mediaItem?.title
info[MPMediaItemPropertyPlaybackDuration] = nowPlayingItem.mediaItem?.playbackDuration
info[MPMediaItemPropertyArtwork] = nowPlayingItem.mediaItem?.artwork()
if self.playbackState != .playing {
info[MPNowPlayingInfoPropertyPlaybackRate] = 1e-6
} else {
info[MPNowPlayingInfoPropertyPlaybackRate] = 1
}
let sec: TimeInterval = CMTimeGetSeconds(time)
info[MPNowPlayingInfoPropertyElapsedPlaybackTime] = Int(sec)
MPNowPlayingInfoCenter.default().nowPlayingInfo = info as? [String: Any]
} else {
if !MoviePlayerController.instance.isPlaying() {
MPNowPlayingInfoCenter.default().nowPlayingInfo = Dictionary()
}
}
}
Это дескриптор RemoteCommandEvent:
let center: MPRemoteCommandCenter = MPRemoteCommandCenter.shared()
center.pauseCommand.addTarget(self, action: #selector(remoteCommandPause(_ :)))
center.playCommand.addTarget(self, action: #selector(remoteCommandPlay(_ :)))
center.stopCommand.addTarget(self, action: #selector(remoteCommandStop(_ :)))
center.togglePlayPauseCommand.addTarget(self, action: #selector(remoteCommandTogglePlayPause(_ :)))
center.nextTrackCommand.addTarget(self, action: #selector(remoteCommandNextTrack(_ :)))
center.previousTrackCommand.addTarget(self, action: #selector(remoteCommandPreviousTrack(_ :)))
center.nextTrackCommand.isEnabled = true
center.previousTrackCommand.isEnabled = true
center.changeShuffleModeCommand.addTarget(self, action: #selector(remoteCommandPause(_ :)))
center.likeCommand.addTarget(self, action: #selector(remoteCommandPause(_ :)))
center.changeRepeatModeCommand.addTarget(self, action: #selector(remoteCommandPause(_ :)))
center.changeShuffleModeCommand.isEnabled = true
center.likeCommand.isEnabled = true
center.changeRepeatModeCommand.isEnabled = true