Search code examples
swift3metadatampmediaitemmpmediaplayercontrollernscfnumber

Can't get key value as String from metadata


I'm grabbing metadata from an MPMediaItem like this:

let url = item.value(forProperty:MPMediaItemPropertyAssetURL) as? NSURL
let asset = AVURLAsset(url: url! as URL, options: nil)
let metaArray = asset.metadata
for metadata in metaArray{
    print("-----metadata:\(metadata)")
    print("-----metadata.key:\(String(describing: metadata.key))")
}

However, when I get a block of metadata printed the "key" is printed as a numeric value instead of "pcst" as shown in the printout:

-----metadata:<AVMetadataItem: 0x1740153f0, identifier=itsk/pcst, keySpace=itsk, key class = __NSCFNumber, key=pcst, commonKey=(null), extendedLanguageTag=(null), dataType=com.apple.metadata.datatype.int8, time={INVALID}, duration={INVALID}, startDate=(null), extras={ dataLength = 1; dataType = 21; dataTypeNamespace = "com.apple.itunes"; }, value=1> -----metadata.key:Optional(1885565812)

This is happening for all of the metadata/keys (there are 29 in this particular media item).

Also note that this line of code:

let realString = NSString(string: metadata.key! as! String)

causes this error: Could not cast value of type '__NSCFNumber' (0x1b80dcdf0) to 'NSString' (0x1b80edae8).

How can I get the string value for the key ("pcst") ?


Solution

  • May be what you are looking for is identifier property of AVMetadataItem.

    for metadata in metaArray{
        print(metadata.identifier ?? "DefaultValue")       
    }