iOS App - Set custom thumbnail to AVPlayer

Hi,

we're trying to set a custom thumbnail image from URL to an AVPlayer on our iOS app. We're trying to use external metadata of a AVPlayerItem, but it doesn't work.


let thumbailData = // image jpg data downloaded from internet
let videoUrl = // HLS video url

// AVURLAsset creation with HLS url
let avAsset = AVURLAsset(url: videoUrl)

// AVPlayerItem with AVURLAsset we've just created
let avPlayerItem = AVPlayerItem(asset: avAsset)

// retrieve external metadatas of AVPlayerItem
var metadata = avPlayerItem.externalMetadata

// Creation new metadata for thumbnail
let thumbnailMetadata = AVMutableMetadataItem()
thumbnailMetadata.keySpace = AVMetadataKeySpace.common
thumbnailMetadata.key = AVMetadataKey.commonKeyArtwork as NSString
thumbnailMetadata.identifier = AVMetadataIdentifier.commonIdentifierArtwork

thumbnailMetadata.dataType = kCMMetadataBaseDataType_JPEG as String
thumbnailMetadata.value = thumbnailData as NSData
thumbnailMetadata.extendedLanguageTag = "und" as String
    
if let metadataItem = thumbnailMetadata.copy() as? AVMetadataItem {
    metadata.append(metadataItem)
}

// AVPlayer creation
let player = AVPlayer(playerItem: avPlayerItem)

// set player on AVPlayerViewController
playerVc.player = player

Any suggestion? Is it the right way to reach our goal?

Thank you

It looks like you're not setting the changed metadata on the player item:

avPlayerItem.externalMetadata = metadata

exterrnalMetadata starts out empty, so unless you're adding external metadata elsewhere in your app, you could just start with a new array here. If you modify this property in multiple places, then you may need to make sure you aren't adding multiple artwork items (as only the first matching one will be used).

iOS App - Set custom thumbnail to AVPlayer
 
 
Q