当AVPlayer'空闲'时,tvOS并显示艺术作品和标题?

时间:2016-10-23 16:02:47

标签: swift tvos

当玩家没有用户输入并且tvOS显示其播放器视图时,我正在尝试制作如何显示专辑插图和标题?基本上是具有半透明背景和通用艺术品的屏幕。应用程序名称显示在我希望显示歌曲信息的位置。

目前我已经创建了一个用于播放音频的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是否是正确的属性用于此?

1 个答案:

答案 0 :(得分:1)

进行更多研究,似乎我需要使用“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()