RugDealer RugDealer - 2 months ago 27
Swift Question

How to set the identifier of AVMetadataItem in swift3?

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?

Answer

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

let titleItem =  AVMutableMetadataItem()
titleItem.identifier = AVMetadataCommonIdentifierTitle