[英]how to get the physical file location of an song in iPod library in iOS
当用户从iPod库中选择音频时,我具有MPMediaItem的引用。 我通过使用获取该项目的资产网址
[mediaItem valueForProperty: MPMediaItemPropertyAssetURL]
但这并没有给我文件的确切物理位置,而是给了我一个带iPod库的URL。
ipod-library://item/item.mp3?id=1840064795502796074
有没有办法从iPod库获取歌曲的物理URL?
编辑 -实际上我想从物理文件中提取NSData并将其发送到我的后端服务器,所以我需要物理文件URL而不是相对URL
这可以工作,但是有点慢。 另外,它是为新的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 {
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.