Writing ID3 tags via AVMetaDataItem

99封情书 提交于 2019-12-19 03:57:04

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/33582845/writing-id3-tags-via-avmetadataitem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!