Andre M
Andre M

Reputation: 7536

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

I am trying to work out how to display the album artwork and title when the player hasn't had user input for a bit and the tvOS displays its player view? Basically the screen with the semi-transparent background and generic artwork. The app name is being displayed where I would expect the song information to be displayed.

Currently I have created a tvOS app for playing audio, and successfully display the artwork and title on the main screen by means of adding my own label, via a subview:

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

For the details in the slide down panel, I use:

    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)

This does not appear to impact the generic screen. I see Apple's Music player displays the artwork track title and album name, so I am wondering whether the playerItem!.externalMetadata is the right attribute to be using for this?

Upvotes: 0

Views: 1131

Answers (1)

Andre M
Andre M

Reputation: 7536

A bit more research and it seems that I need to use the '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()

Upvotes: 0

Related Questions