swiftmacositunesmedia-library

How do I access the title of an iTunes song using MLMediaLibrary and Swift on macOS?


I'm using Apple's MediaLibrary framework on macOS, to write an application that reads in information on every song in my iTunes library.

I'm able to extract the artist of a song, using MLMediaObjectArtistKey as a key in the attributes dictionary (property on MLMediaObject), but how do I access the title of the song?

From what I can tell from the documentation, it seems that this information (the title) simply is not provided through the attributes dictionary.

Have others had any luck retrieving the title, and if so, how?


Solution

  • On a MLMediaObject:

    print("\(mediaObj.attributes[MLMediaObjectArtistKey]!), \(mediaObj.name!)") // Swift
    

    mediaObj.attributes["Name"]


    To get the keys from a dictionary:

    print(Array(mediaObj.attributes.keys)) // Swift