In my app I want to export files from Music Library and save them to Documents directory. I already fetch file list from Music Library and I get url of each file. But I still have two problems:
I can export files only in "m4a" format, but I'd like to export it in format what file was in Music Library (mp3, wav, aac or else)
How can I save files from Music Library to Documents directory with url? My code now looks like this:
private func exportFile(with assetURL: URL, completionHandler: @escaping (_ fileURL: URL?, _ error: Error?) -> ()) { let asset = AVURLAsset(url: assetURL) guard let exporter = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetAppleM4A) else { completionHandler(nil, nil) return } let fileURL = URL(fileURLWithPath: NSTemporaryDirectory()) .appendingPathComponent(NSUUID().uuidString) .appendingPathExtension("m4a") exporter.outputURL = fileURL exporter.outputFileType = AVFileType.m4a exporter.exportAsynchronously { if exporter.status == .completed { completionHandler(fileURL, nil) } else { completionHandler(nil, exporter.error) } } func downloadFile(file: MPMediaItem?, callback: @escaping () -> ()) { guard let file = file, let url = file.url else { return } exportFile(with: url) { fileURL, error in guard let fileURL = fileURL, error == nil else { print(error?.localizedDescription) return } //Here I'd like to save file to Documents dir print("\(fileURL)") } callback() }
Solution for second question.
I put the code in one method and save directly to the Documents folder, not to a temporary folder: