3

Description:

I'm attempting to set the identifier of AVMetadataItem in swift3, like mentioned in this apple video. However, the video is pre-swift3.

In swift2 it would look something like this:

let metadataItem = AVMetadataItem(identifier: AVMetadataCommonIdentifierTitle, value: "Title here")

However, in swift3 that constructor does not exist anymore.

There is an empty constructor and this one:

AVMetadataItem(propertiesOf: AVMetadataItem, valueLoadingHandler: (AVMetadataItemValueRequest) -> Void)

There does not seem to be any methods that exist to set the identifier.

Question:

How do I set the identifier of AVMetadataItem in swift3?

Edit:

Tried with AVMutableMetadataItem() as suggested by Lucas.

private func setupNavigationMarker(title: String, description: String, timeRange: CMTimeRange)-> AVTimedMetadataGroup {
    var items: [AVMetadataItem] = []
    let titleItem =  AVMutableMetadataItem()
    titleItem.identifier = AVMetadataCommonIdentifierTitle
    titleItem.value = title as (NSCopying & NSObjectProtocol)?
    items.append(titleItem)

    let descriptionItem = AVMutableMetadataItem()
    descriptionItem.identifier = AVMetadataCommonIdentifierDescription
    descriptionItem.value = description as (NSCopying & NSObjectProtocol)?
    items.append(descriptionItem)

    return AVTimedMetadataGroup(items: items, timeRange: timeRange)

}

and here in use:

    let cmTimeStart = CMTimeMake(0, 0)
    let cmTimeDuration = CMTimeMake(10, 1)

    let timeRange = CMTimeRange(start: cmTimeStart, duration: cmTimeDuration)

    let timedMetadataGroup = self.setupNavigationMarker(title: "test", description: "description test" , timeRange: timeRange)
    let timedMetadataGroupList = [timedMetadataGroup]
    let navigationMarkersGroup = AVNavigationMarkersGroup(title: "Chapters", timedNavigationMarkers: timedMetadataGroupList)
    //predefined avPlayerItem
    avPlayerItem.navigationMarkerGroups.append(navigationMarkersGroup)

It compiles and runs but no navigation markers (aka chapters) are proposed when the video plays. What am I missing?

Ketan P
  • 4,259
  • 3
  • 30
  • 36
kemicofa ghost
  • 16,349
  • 8
  • 82
  • 131

1 Answers1

3

Use AVMutableMetadata instead of AVMetadata this way you can modify the identifier as you choose to.

let titleItem =  AVMutableMetadataItem()
titleItem.identifier = AVMetadataCommonIdentifierTitle
kemicofa ghost
  • 16,349
  • 8
  • 82
  • 131
Luca Davanzo
  • 21,000
  • 15
  • 120
  • 146
  • Have you try using AVMutableMetadataItem instance? "To create metadata items for your own assets, you use the mutable subclass, AVMutableMetadataItem." – Luca Davanzo Sep 21 '16 at 09:36
  • @LucasDavanzo I actually have not. I'll play around with it and see if it works. Thanks for the advice. – kemicofa ghost Sep 21 '16 at 09:39