我正在使用以下代码将音乐文件从Apple音乐库导出到我的文档中:
func exportMusic(_ filename: String,_ dir: String,_ assetURL: URL, completionHandler: @escaping (_ fileURL: URL?, _ error: Error?) -> ()) {
let asset = AVURLAsset(url: assetURL)
guard let exporter = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetAppleM4A) else {
completionHandler(nil, ExportError.unableToCreateExporter)
return
}
print(exporter.supportedFileTypes)
if !self.createDirectory(dir) {
completionHandler(nil, ExportError.unableToCreateExporter)
return
}
let fileURL = self.fixPath
.appendingPathComponent("\(dir)/")?
.appendingPathComponent(filename)
.appendingPathExtension("m4a")
exporter.outputURL = fileURL
exporter.outputFileType = AVFileType(rawValue: "com.apple.m4a-audio")
exporter.exportAsynchronously {
if exporter.status == .completed {
completionHandler(fileURL, nil)
} else {
completionHandler(nil, exporter.error)
}
}
}
enum ExportError: Error {
case unableToCreateExporter
}
可以,但是当我打开此音乐文件时,所有音乐元数据(如封面图像,歌曲标题,专辑标题和歌手)已被删除。
如何导出包含所有元数据的音乐文件?