i had tried the below code.
-(void)mediaItemToData : (MPMediaItem * ) curItem
{
NSURL *url = [curItem valueForProperty: MPMediaItemPropertyAssetURL];
SWIFT 3 Version from @Hitarth
func save(mediaItem: MPMediaItem) -> String? {
let url = mediaItem.assetURL!
let hex = MD5Hex(string: url.absoluteString)
guard didMD5HexExist(string: hex) == false else {
return hex
}
let songAsset = AVURLAsset(url: url)
guard let exporter = AVAssetExportSession(asset: songAsset, presetName: AVAssetExportPresetAppleM4A) else {
assertionFailure()
return nil
}
exporter.outputFileType = "com.apple.m4a-audio"
let fileHexName = hex + ".m4a"
let fileURL = latFileManager.getResourceFolderPath().appendingPathComponent(fileHexName)
DLog("fileURL: \(fileURL)")
exporter.outputURL = fileURL
// do the export
exporter.exportAsynchronously {
let status = exporter.status
switch status {
case .failed:
assertionFailure(exporter.error as! String)
case .completed:
DLog("AVAssetExportSessionStatusCompleted")
self.fileHexArray.append(fileHexName)
default:
DLog("default")
break
}
}
return hex
}