tvOS 并在 AVPlayer 'idles' 时显示插图和标题?

tvOS and displaying artwork and title when AVPlayer 'idles'?

我正在尝试弄清楚当播放器暂时没有用户输入并且 tvOS 显示其播放器视图时如何显示专辑插图和标题?基本上是带有 semi-transparent 背景和通用艺术作品的屏幕。应用程序名称显示在我希望显示歌曲信息的位置。

目前我已经创建了一个用于播放音频的 tvOS 应用程序,并通过子视图添加我自己的标签成功地在主屏幕上显示了插图和标题:

   let albumArtView = UIImageView(image: image)
   self.contentOverlayView?.addSubview(albumArtView)

对于下拉面板中的详细信息,我使用:

    playerItem = AVPlayerItem(url: videoURL!)

    // Add station title

    let titleMetadataItem = AVMutableMetadataItem()
    titleMetadataItem.locale = Locale.current
    titleMetadataItem.key = AVMetadataCommonKeyTitle as (NSCopying & NSObjectProtocol)?
    titleMetadataItem.keySpace = AVMetadataKeySpaceCommon
    titleMetadataItem.value = stationName as (NSCopying & NSObjectProtocol)?

    playerItem!.externalMetadata.append(titleMetadataItem)

    // Add station description

    let descriptionMetadataItem = AVMutableMetadataItem()
    descriptionMetadataItem.locale = Locale.current
    descriptionMetadataItem.key = AVMetadataCommonKeyDescription as (NSCopying & NSObjectProtocol)?
    descriptionMetadataItem.keySpace = AVMetadataKeySpaceCommon
    descriptionMetadataItem.value = stationDescription as (NSCopying & NSObjectProtocol)?

    playerItem!.externalMetadata.append(descriptionMetadataItem)

    // Add station artwork

    let image = UIImage(named: "stationAlbumArt")
    let artworkMetadataItem = AVMutableMetadataItem()
    artworkMetadataItem.locale = Locale.current
    artworkMetadataItem.identifier = AVMetadataCommonIdentifierArtwork
    artworkMetadataItem.value = UIImagePNGRepresentation(image!) as (NSCopying & NSObjectProtocol)?
    playerItem!.externalMetadata.append(artworkMetadataItem)

这似乎不会影响通用屏幕。我看到 Apple 的音乐播放器显示了艺术作品曲目标题和专辑名称,所以我想知道 playerItem!.externalMetadata 是否是为此使用的正确属性?

进一步研究,我似乎需要使用 'MPNowPlayingInfoCenter':

let title = "track title"
let artist = "artist name"
let artwork = MPMediaItemArtwork(image: UIImage(named: "stationAlbumArt")!)
let nowPlayingInfo = [MPMediaItemPropertyArtist : artist,  MPMediaItemPropertyTitle : title, MPMediaItemPropertyArtwork : artwork] as [String : Any]
MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
UIApplication.shared.beginReceivingRemoteControlEvents()