播放歌曲时播放/暂停按钮不更新

问题描述

我正在构建一个可以与 CarPlay 集成的音乐应用程序,一切正常...我可以在音乐播放器上显示艺术家姓名、标题、专辑名称和一些按钮控制器。但是,有一个问题,当我从 CarPlay 播放任何歌曲时,CarPlay 上的按钮与 iOS 设备上的按钮不同,并且寻道时间不移动。

我该怎么办?我搜索了很多资源以找到解决方案,但仍然没有。

enter image description here

enter image description here

注意:我只是在模拟器上测试(不是真机)。

这是播放信息:

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

解决方法

您是否曾在代码中注册过远程事件?例如

UIApplication.shared.beginReceivingRemoteControlEvents()

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...