如何在 iOS 的 iPod 库中获取歌曲的物理文件位置
Posted
技术标签:
【中文标题】如何在 iOS 的 iPod 库中获取歌曲的物理文件位置【英文标题】:how to get the physical file location of an song in iPod library in iOS 【发布时间】:2013-01-08 12:08:08 【问题描述】:当用户从 iPod 库中选择一个音频时,我有一个 MPMediaItem 的引用。我正在使用
获取该项目的资产网址[mediaItem valueForProperty: MPMediaItemPropertyAssetURL]
但这并没有给我文件的确切物理位置,而是给我一个 url w.r.t iPod 库。
ipod-library://item/item.mp3?id=1840064795502796074
有没有办法从 iPod 库中获取歌曲的物理 url?
编辑 - 实际上我想从物理文件中提取 NSData 并将其发送到我的后端服务器,所以我需要物理文件 URL 而不是相对 URL
【问题讨论】:
它总是返回一个 URL 以在另一个媒体播放器中播放 实际上我想从物理文件中提取 NSData 并将其发送到我的后端服务器,所以我需要物理文件 URL 而不是相对 URL 它不会提供绝对路径,它总是返回 URl 或相对路径。我对此进行了很多搜索,但一无所获。我也尝试从 url 制作数据,但它给出了 0 个字节 ooooh.. 那么,您是如何将音频文件转换为 NSData 的呢? 我没有,我们留下了那个功能...... 【参考方案1】:这会起作用,但它有点慢。此外,它是为新的 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
【讨论】:
以上是关于如何在 iOS 的 iPod 库中获取歌曲的物理文件位置的主要内容,如果未能解决你的问题,请参考以下文章