首页 > 解决方案 > 无法在 Carplay 音乐上显示专辑封面

问题描述

我正在构建一个可以与Apple Carplay集成的iOS音乐应用程序,我可以在Carplay上正常播放音乐,并且可以显示歌曲的一些信息,如标题、专辑、艺术家姓名。但是无法显示专辑封面。

这是在 Carplay 上显示媒体信息的批量代码:

     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()
        let sec: TimeInterval = CMTimeGetSeconds(time)
        info[MPNowPlayingInfoPropertyElapsedPlaybackTime] = Int(sec)
        MPNowPlayingInfoCenter.default().nowPlayingInfo = info as? [String: Any]
    }

这是我当前的应用程序:

在此处输入图像描述

这就是我想要的:

在此处输入图像描述

那我该怎么办?请帮我找到解决方案。

标签: swiftios14mpnowplayinginfocentercarplayartwork

解决方案


您可能正在模拟器上进行测试。虽然有时需要以下代码来显示正在播放的屏幕,但目前无法让 CarPlay 模拟器确定玩家实际正在播放:

#if targetEnvironment(simulator)
    UIApplication.shared.endReceivingRemoteControlEvents()
    UIApplication.shared.beginReceivingRemoteControlEvents()
#endif

我无法让专辑插图显示在模拟器中正在播放的屏幕上。您是否可以使用实体车载收音机(我强烈建议您在提交到 App Store 之前对其进行测试)?我知道这可能不是很有说服力,但如果你的作品出现在 iPhone 的通知中心播放器中,它也会出现在 CarPlay 中(在真实设备上),因为现在正在播放的组件只是被代理了。


推荐阅读