1

当用户从 iPod 库中选择音频时,我有 MPMediaItem 的引用。我通过使用获取该项目的资产网址

[mediaItem valueForProperty: MPMediaItemPropertyAssetURL]

但这并没有给我文件的确切物理位置,而是给了我一个 url wrt iPod 库。

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

有没有办法从 iPod 库中获取歌曲的物理 url?

编辑- 实际上我想从物理文件中提取 NSData 并将其发送到我的后端服务器,所以我需要物理文件 URL 而不是相对 URL

4

1 回答 1

0

这会起作用,但它有点慢。此外,它是为新的 MP 类编写的:

// 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 {

       }
}
于 2017-05-23T01:14:42.937 回答