Exporting MP3 with AVAssetExportSession

1.4k Views Asked by At

I now have this chunk of Swift code in an iOS application, that allows me to transfer some audio item from the iTunes library of my device to the Document Directory of my own app.

let exportSession = AVAssetExportSession(asset: AVAsset(url: url), 
                                         presetName: AVAssetExportPresetAppleM4A)
exportSession?.shouldOptimizeForNetworkUse = true
exportSession?.outputFileType = AVFileType.m4a
exportSession?.outputURL = newFileURL

exportSession?.exportAsynchronously(completionHandler: {[weak self]
    () -> Void in
    if exportSession!.status == AVAssetExportSession.Status.completed  {
        // All is working fine!!
        .... some useful code ....
    }
}

It works fine with an .m4a type of media item, but unfortunately not with an .mp3 type of media.

I have of course, tried variations of this code, but none of them worked.

That would be great if someone had a tip for making it work.

1

There are 1 best solutions below

3
Michel On BEST ANSWER

In case someone else faces the same question; here is what ended up by working for me.

It seems like this is not the only possible solution, but I did not try any other.

let exportSession = AVAssetExportSession(asset: AVAsset(url: url), 
                                         presetName: AVAssetExportPresetPassthrough)
exportSession?.shouldOptimizeForNetworkUse = true
exportSession?.outputFileType = AVFileType.caf
exportSession?.outputURL = newFileURL // File with a .caf extention.

exportSession?.exportAsynchronously(completionHandler: {[weak self]
    () -> Void in
    if exportSession!.status == AVAssetExportSession.Status.completed  {
        // All is working fine!!
        .... some useful code ....
    }
}