1

ユーザーが iPod ライブラリからオーディオを選択すると、MPMediaItem が参照されます。を使用して、そのアイテムのアセット URL を取得しています

[mediaItem valueForProperty: MPMediaItemPropertyAssetURL]

しかし、これはファイルの正確な物理的な場所を教えてくれるのではなく、iPod ライブラリに関する URL を教えてくれます。

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 に答える