I am able to get some information from audio track but not track id. Anyone point me to how to get track id? I am using xcode 12.3. Thanks!
func getM4aAudioDetails(afile: URL) {
let song = Song(context: moc)
let avasset = AVAsset(url: afile as URL) as AVAsset
let metadata = avasset.metadata(forFormat: AVMetadataFormat.iTunesMetadata)
let cmetadata = avasset.commonMetadata
let titleItems = AVMetadataItem.metadataItems(from: metadata, withKey: "?nam", keySpace: AVMetadataKeySpace(rawValue: "itsk"))
if let data = titleItems.first, let title = data.stringValue {
song.title = title
}
let artistItems = AVMetadataItem.metadataItems(from: metadata, withKey: "?ART", keySpace: AVMetadataKeySpace(rawValue: "itsk"))
if let data = artistItems.first, let artist = data.stringValue {
song.artist = artist
}
let albumItems = AVMetadataItem.metadataItems(from: metadata, withKey: "?alb", keySpace: AVMetadataKeySpace(rawValue: "itsk"))
if let data = albumItems.first, let album = data.stringValue {
song.album = album
}
let trackItems = AVMetadataItem.metadataItems(from: metadata, withKey: "trkn", keySpace: AVMetadataKeySpace(rawValue: "itsk"))
if let data = trackItems.first, let trackString = data.stringValue {
song.track = Int64(trackString)!
print("Track: (song.track)")
}
let artworkItems = AVMetadataItem.metadataItems(from: cmetadata, filteredByIdentifier: .commonIdentifierArtwork)
if let artworkItem = artworkItems.first {
if let imageData = artworkItem.dataValue {
//let image = NSImage(data: imageData)
song.artwork = imageData as Data
//print("image found")
}
}
print("song title: (song.title)")
print("song artist: (song.artist)")
print("song name: (song.album)")
print("song track: (song.track)")
}
Prints: (excerpt)
song title: Loser
song artist: Beck
song name: Mellow Gold
song track: 0
song title: Jack-Ass
song artist: Beck
song name: Odelay
song track: 0
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…