1
votes

I have the reference of a MPMediaItem when user selects a n audio from the iPod library. i am getting the asset url of that item by using

[mediaItem valueForProperty: MPMediaItemPropertyAssetURL]

But this is not giving me the exact physical location of the file, instead it is giving me an url w.r.t iPod library.

ipod-library://item/item.mp3?id=1840064795502796074

Is there a way to get the physical url of a song from iPod library?

EDIT - actually i want to extract NSData from the physical file and send it to my backend server, so i need the physical file URL and not the relative URL

1
it always returned you a URL to play in another media playerAman Aggarwal
actually i want to extract NSData from the physical file and send it to my backend server, so i need the physical file URL and not the relative URLA for Alpha
It will not provided you absolute path, it always return URl or relative path. I made a lot of search regarding this but got nothing. I also tried to make a data from url but it gave 0 bytesAman Aggarwal
ooooh.. So, how did you manage to convert the audiofile to NSData?A for Alpha
i did no, we left that functionality...Aman Aggarwal

1 Answers

0
votes

This will work, but it's a bit slow. Also, it's written for the new MP classes:

// song is an instance of MPMediaItem

if let val = song.value(forKey: MPMediaItemPropertyAssetURL) as? URL {
   let asset = AVURLAsset.init(url: val)

   if asset.isExportable {
      let exportSession = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetAppleM4A)

      let exportPath: NSString = NSTemporaryDirectory().appendingFormat("/\(UUID().uuidString).m4a") as NSString
      let exportUrl: NSURL = NSURL.fileURL(withPath: exportPath as String) as NSURL

      exportSession?.outputURL = exportUrl as URL
      exportSession?.outputFileType = AVFileTypeAppleM4A
      exportSession?.exportAsynchronously(completionHandler: {
          // do some stuff with the file
          do {
            try FileManager.default.removeItem(atPath: exportPath as String!)
          } catch {

       }
}