Search code examples
iosswiftmetadataavplayer

Adding metadata to an AVPlayerItem in Swift 3


I am attempting to associate a title with an AVPlayerItem and cannot seem to get it working. According to Apple's documentation, I should be able to use AVPlayerItem.externalMetadata.append(value) to accomplish this, but I cannot seem to get it to work. The compiler returns the message "Value of type 'AVPlayerItem' has no member 'externalMetadata.'" As I have done a lot of searching and have not found anyone with similar issues, I assume I must be missing something obvious. This is the function I am attempting to use:

func enqueue(avAsset: AVURLAsset, title:String) {
    let item = AVPlayerItem(asset: avAsset)

    let titleMetadata = AVMutableMetadataItem()
    titleMetadata.identifier = AVMetadataCommonKeyTitle
    titleMetadata.value = title as (NSCopying & NSObjectProtocol)?
    item.externalMetadata.append(titleMetadata)
    self.player.insert(item, after: nil)
    if player.rate == 0 {
        self.startPlaying()
    }
}

Any help would be greatly appreciated.


Solution

  • externalMetadata is a tvOS only property. In the right-hand column of the API Reference, under the SDK section, there is only "tvOS 9.0+" there. It is simply not available to iOS.

    Update: The property now supports iOS 12.0+ also. You should be able to use it now.