I'm writing ID3 tags to a file using AVMetaDataItem
var soundFileMetadata = [AVMetadataItem]()
soundFileMetadata.append(createMetadata(AVMetadataiTunesMetadataKeyArtist, "MyArtist")!)
soundFileMetadata.append(createMetadata(AVMetadataiTunesMetadataKeySongName, "MySong")!)
soundFileMetadata.append(createMetadata(AVMetadataiTunesMetadataKeyAlbum, "MyAlbum")!)
soundFileMetadata.append(createMetadata(AVMetadataiTunesMetadataKeyUserGenre, "MyGenre")!)
soundFileMetadata.append(createMetadata(AVMetadataiTunesMetadataKeyComposer, "MyComposer")!)
Here is the createMetadata
convenience method:
func createMetadata(tagKey: String, _ tagValue: AnyObject?,
keySpace:String = AVMetadataKeySpaceiTunes) -> AVMutableMetadataItem? {
if let tagValue = tagValue {
let tag = AVMutableMetadataItem()
tag.keySpace = keySpace
tag.key = tagKey
tag.value = (tagValue as? String) ?? (tagValue as? Int)
return tag
}
return nil
}
I then tried to write also the year tag, with no success:
let comps = NSDateComponents()
comps.year = 2010;
let yearTag = AVMutableMetadataItem()
yearTag.keySpace = AVMetadataKeySpaceID3
yearTag.key = AVMetadataID3MetadataKeyYear
yearTag.value = NSCalendar.currentCalendar().dateFromComponents(comps)
soundFileMetadata.append(yearTag)
In this case I get this error:
FigMetadataCreateConverter signalled err=-12482 (kFigMetadataConverterError_UnsupportedFormat) (Unsupported format conversion) at /SourceCache/CoreMedia/CoreMedia-1562.238/Prototypes/Metadata/Converters/FigMetadataConverterCommon.c line 118
Note that this is a simple error printed in console, not an exception!
Also writing it as a String
, as an Int
o even a Float
, leads me to the same error.
Same is for Track/Disc count, Track/Disc number tags.
First question is: how to write them?
I also have another question.
Currently I've an AVAudioRecorder
, I found no way to write tags directly to the output file of the recorder, so I commit the recorder file, open it with AVURLAsset
and re-export it with AVAssetExportSession
:
self.recorder.stop()
let urlAsset = AVURLAsset(URL: srcSoundFileURL)
let assetExportSession: AVAssetExportSession! = AVAssetExportSession(asset: urlAsset, presetName: AVAssetExportPresetPassthrough)
assetExportSession.outputFileType = AVFileTypeAppleM4A
assetExportSession.outputURL = tmpSoundFileURL
assetExportSession.metadata = soundFileMetadata
assetExportSession.exportAsynchronouslyWithCompletionHandler({
....
})
Second question is: is there any way to avoid this double-step action?
I've managed to add the year tag with your code with a few modifications:
let yearTag = AVMutableMetadataItem()
yearTag.keySpace = AVMetadataKeySpaceiTunes
yearTag.key = AVMetadataiTunesMetadataKeyReleaseDate
yearTag.value = "2123"
I couldn't make it work with the ID3 keys so I thought this could be the problem, and indeed it works with these iTunes keys. Also, the value has to be a String (or NSString), not a date object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With