2

I'm building an music app that can integrate with CarPlay, everything is all right... I can display artist name, title, album name and some button controller on music player. However, there is an issue that when i play any song from CarPlay, the button on CarPlay is not update same as button on iOS device and seek time does not move.

What should I do ? I searched many resources to find solution but still yet.

enter image description here

enter image description here

Note: I just only test it on simulator (not real device).

This is show 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()
        }
    }
}

This is handle 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
Le Minh
  • 135
  • 1
  • 1
  • 12

1 Answers1

0

Do you ever register for the remote events in your code? e.g

UIApplication.shared.beginReceivingRemoteControlEvents()
fruitcoder
  • 1,073
  • 8
  • 24
  • Yes, i registered for the remote events control when play music. UIApplication.shared.endReceivingRemoteControlEvents() UIApplication.shared.beginReceivingRemoteControlEvents() completionHandler(nil) – Le Minh Jan 23 '21 at 09:28
  • 1
    i found a solution, that is set **if #available(iOS 13, *) { MPNowPlayingInfoCenter.default().playbackState = isPlaying ? .playing : .paused }** when show Now Playing Info screen, however this statement is valid on iOS 14 not iOS 13 although I checked version iOS before set it. – Le Minh Jan 26 '21 at 10:03
  • So one thing that isn't working on the simulator ist the play button (it will never change to a pause button like it would on a real device). Before you release you should definitely check your implementation on a real device. As for the elapsed time: Can you check that you set the `MPNowPlayingInfoPropertyElapsedPlaybackTime` every second? The progress should actually be the same as on iOS in the notification center. During the implementation we also found some weird behavior which we documented here: https://medium.com/br-next/launching-br-radio-on-carplay-audio-8baab824b932 Maybe that helps – fruitcoder Jan 26 '21 at 10:51