通过AVMetaDataItem编写ID3标签 [英] Writing ID3 tags via AVMetaDataItem

查看:127
本文介绍了通过AVMetaDataItem编写ID3标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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")!)

这是createMetadata便捷方法:

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

请注意,这是在控制台上打印的简单错误,不是一个例外!

也将其写为StringInt甚至是Float,也会导致我遇到相同的错误. 曲目/唱片计数,曲目/唱片编号标签也是如此.

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.

第一个问题是:如何编写它们?

我还有一个问题.

当前我有一个AVAudioRecorder,我发现无法直接将标签写入记录器的输出文件,因此我提交了记录器文件,使用AVURLAsset打开它,然后使用AVAssetExportSession重新导出:

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({ 
         ....

})

第二个问题是:有什么办法可以避免这种双重措施?

推荐答案

通过一些修改,我设法在代码中添加了year标记:

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"

我无法使其与ID3密钥一起使用,因此我认为这可能是问题所在,并且确实可以与这些iTunes密钥一起使用.另外,该值必须是String(或NSString),而不是日期对象.

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.

这篇关于通过AVMetaDataItem编写ID3标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆