i had tried the below code.
-(void)mediaItemToData : (MPMediaItem * ) curItem
{
NSURL *url = [curItem valueForProperty: MPMediaItemPropertyAssetURL];
Try this one written in Swift4 and reason of using AVAssetExportSession https://stackoverflow.com/a/36694392/5653015
func mediaPicker(_ mediaPicker: MPMediaPickerController, didPickMediaItems mediaItemCollection: MPMediaItemCollection)
{
//get media item first
guard let mediaItem = mediaItemCollection.items.first else
{
NSLog("No item selected.")
return
}
let songUrl = mediaItem.value(forProperty: MPMediaItemPropertyAssetURL) as! URL
print(songUrl)
// get file extension andmime type
let str = songUrl.absoluteString
let str2 = str.replacingOccurrences( of : "ipod-library://item/item", with: "")
let arr = str2.components(separatedBy: "?")
var mimeType = arr[0]
mimeType = mimeType.replacingOccurrences( of : ".", with: "")
let exportSession = AVAssetExportSession(asset: AVAsset(url: songUrl), presetName: AVAssetExportPresetAppleM4A)
exportSession?.shouldOptimizeForNetworkUse = true
exportSession?.outputFileType = AVFileType.m4a
//save it into your local directory
let documentURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let outputURL = documentURL.appendingPathComponent(mediaItem.title!)
print(outputURL.absoluteString)
//Delete Existing file
do
{
try FileManager.default.removeItem(at: outputURL)
}
catch let error as NSError
{
print(error.debugDescription)
}
exportSession?.outputURL = outputURL
exportSession?.exportAsynchronously(completionHandler: { () -> Void in
if exportSession!.status == AVAssetExportSessionStatus.completed
{
print("Export Successfull")
// self.getAudio()
}
self.dismiss(animated: true, completion: nil)
})
}