CodyMace
CodyMace

Reputation: 655

AVPlayer metadata artwork does not show on tvOS

I’m trying to display Info on an AVPlayerController dropdown. I’m following exactly what they show in WWDC, adding metadata to the playerItem. Everything is showing up ok, except for the artwork image. What’s strange is that it seems to add the image to the info view, but then hides it. There’s a space where the image should be but nothing shows. It does show up on the tv remote app though, so I know I’m adding it correctly. Here’s my code:

let playerItem: AVPlayerItem = AVPlayerItem(asset: avasset)
var allItems: [AVMetadataItem] = []
allItems.append(self.metadataItem(identifier: AVMetadataCommonIdentifierTitle, value: asset.title as (NSCopying & NSObjectProtocol)?)!)
if let desc = asset.desc {
    allItems.append(self.metadataItem(identifier: AVMetadataCommonIdentifierDescription, value: desc as (NSCopying & NSObjectProtocol)?)!)
}
if let image = self.thumbImage, let artworkItem = self.metadataArtworkItem(image: image) {
    allItems.append(artworkItem)
}
playerItem.externalMetadata = allItems
self.setupPlayerItem(playerItem)

The methods used to create the metadataItem:

func metadataItem(identifier: String, value: (NSCopying & NSObjectProtocol)?) -> AVMetadataItem? {
    if let actualValue = value {
        let item = AVMutableMetadataItem()
        item.value = actualValue
        item.identifier = identifier
        item.extendedLanguageTag = "und"
        return item.copy() as? AVMetadataItem
    }
    return nil
}

func metadataArtworkItem(image: UIImage) -> AVMetadataItem? {
    let item = AVMutableMetadataItem()
    item.value = UIImagePNGRepresentation(image) as (NSCopying & NSObjectProtocol)?
    item.dataType = kCMMetadataBaseDataType_PNG as String
    item.identifier = AVMetadataCommonIdentifierArtwork
    item.extendedLanguageTag = "und"
    return item.copy() as? AVMetadataItem
}

enter image description here

Upvotes: 2

Views: 2201

Answers (2)

C6Silver
C6Silver

Reputation: 3357

Not sure what was happening a few years ago around this problem, but the issue now is if you don't convert your image to a pngData the image won't show. In other words just sending the Artwork item a UIImage won't work. So it needs to be like this per Apple:

if let showImage = image, let pngData = showImage.pngData() {
                let imageItem = self.makeMetadataItem(AVMetadataIdentifier.commonIdentifierArtwork.rawValue, value: pngData)
                metadata.append(imageItem)
            }

Upvotes: 2

CodyMace
CodyMace

Reputation: 655

So it turns out that if the AVMetadataCommonIdentifierDescription item is nil or an empty string, the image gets hidden. All I had to do to fix it was set the description to " " if there's not text to show. I'm going to file a bug with apple on this because that's obviously not normal.

Upvotes: 2

Related Questions