Search code examples
avplayertvosavplayerviewcontroller

tvOS - Override start time / end time slider AVPlayerViewController


I'm using AVPlayerViewController in order to play an HLS file, however the start time is always 00:00 and the end time is the duration of the event from the HLS manifest.

Instead I would wish to display the start time of the event and the end time of the event.

I found that can be used: AVKitMetadataIdentifierExactStartDate / AVKitMetadataIdentifierExactEndDate

But looks like when I create an AVMutableMetadataItem and I try to assign as identifier the AVKitMetadataIdentifierExactStartDate it doesn't exist. So I'm kind of stuck.

Anyone has any idea?


Solution

  • After few days of researches, I found that this can be achieved creating a AVMutableMetadataItem, assigning them as identifier an AVMetadataIdentifier(AVKitMetadataIdentifierExactStartDate) then as value you can just add the start time as date and cast everything as NSCopying & NSObjectProtocol.

    Once you setup both properties you can append to the player.currentItem.externalMetadatas the new metadataItems that you just created as array of metadataItems.

    Full example below:

    //Add start date
            let item = AVMutableMetadataItem()
            item.identifier = AVMetadataIdentifier(AVKitMetadataIdentifierExactStartDate)
            item.value = startDate as? NSCopying & NSObjectProtocol
            let metadataItem = item.copy() as! AVMetadataItem
            
            //Add start date
            let endTimeItem = AVMutableMetadataItem()
            endTimeItem.identifier = AVMetadataIdentifier(AVKitMetadataIdentifierExactEndDate)
            endTimeItem.value = endDate as? NSCopying & NSObjectProtocol
            let endTimeMetadataItem = endTimeItem.copy() as! AVMetadataItem
            
            var metadataItems = [AVMetdataItem]()
            metadataItems.append(metadataItem)
            metadataItems.append(endTimeMetadataItem)
            self.player.currentItem?.externalMetadata = metadataItems