let url = item.valueForProperty(MPMediaItemPropertyAssetURL)@H_404_7@但这并没有给我文件的确切物理位置,相反,它给了我一个URL w.r.t iPod库. @H_404_7@
ipod-library://item/item.mp3?ID=1840064795502796074@H_404_7@有没有办法从iPod库中获取歌曲的物理URL? @H_404_7@编辑 – 实际上我想从物理文件中提取NSData并将其发送到我的后端服务器,所以我需要物理文件URL而不是相对URL @H_404_7@MPmediAPIckerController正在工作,我选择了歌曲及其播放,但我不想播放这首歌.我试图将音频文件上传到服务器.我在列表音频中使用MPMedia Picker视图,当我要选择我将上传到服务器(http)的音频时,我该怎么做?如何使用Swift代码访问媒体库?解决方法 调整Krishna的答案,使用AVAssetExportSession将MPMediaItem保存到文件中,您可以在Swift 3中执行以下 *** 作: @H_404_7@ @H_404_7@
/// Export MPMediaItem to temporary file.////// - Parameters:/// - assetURL: The `assetURL` of the `MPMediaItem`./// - completionHandler: Closure to be called when the export is done. The parameters are a boolean `success`,the `URL` of the temporary file,and an optional `Error` if there was any problem. The parameters of the closure are:////// - fileURL: The `URL` of the temporary file created for the exported results./// - error: The `Error`,if any,of the asynchronous export process.func export(_ assetURL: URL,completionHandler: @escaPing (_ fileURL: URL?,_ error: Error?) -> ()) { let asset = AVURLAsset(url: assetURL) guard let exporter = AVAssetExportSession(asset: asset,presetname: AVAssetExportPresetAppleM4A) else { completionHandler(nil,ExportError.unabletoCreateExporter) return } let fileURL = URL(fileURLWithPath: NstemporaryDirectory()) .appendingPathComponent(NSUUID().uuIDString) .appendingPathExtension("m4a") exporter.outputURL = fileURL exporter.outputfileType = "com.apple.m4a-audio" exporter.exportAsynchronously { if exporter.status == .completed { completionHandler(fileURL,nil) } else { completionHandler(nil,exporter.error) } }}func exampleUsage(with mediaItem: MPMediaItem) { if let assetURL = mediaItem.assetURL { export(assetURL) { fileURL,error in guard let fileURL = fileURL,error == nil else { print("export Failed: \(error)") return } // use fileURL of temporary file here print("\(fileURL)") } }}enum ExportError: Error { case unabletoCreateExporter}@H_404_7@或者,在Swift 2中: @H_404_7@
/// Export MPMediaItem to temporary file.////// - Parameters:/// - assetURL: The `assetURL` of the `MPMediaItem`./// - completionHandler: Closure to be called when the export is done. The parameters are a boolean `success`,of the asynchronous export process.func export(assetURL: NSURL,completionHandler: (NSURL?,ErrorType?) -> ()) { let asset = AVURLAsset(URL: assetURL) guard let exporter = AVAssetExportSession(asset: asset,ExportError.unabletoCreateExporter) return } let fileURL = NSURL(fileURLWithPath: NstemporaryDirectory()) .URLByAppendingPathComponent(NSUUID().UUIDString)! .URLByAppendingPathExtension("m4a") exporter.outputURL = fileURL exporter.outputfileType = "com.apple.m4a-audio" exporter.exportAsynchronouslyWithCompletionHandler { if exporter.status == .Completed { completionHandler(fileURL,error in guard let fileURL = fileURL where error == nil else { print("export Failed: \(error)") return } // use fileURL of temporary file here print("\(fileURL)") } }}enum ExportError: ErrorType { case unabletoCreateExporter}@H_404_7@如您所见,我将其放在临时文件夹而不是documents文件夹中.此外,我使用UUID而不是自某个引用日期以来生成临时文件的秒数.但这个想法基本相同. 总结
以上是内存溢出为你收集整理的ios – 是否可以使用swift将Ipod库中的音乐保存到我的应用程序中?全部内容,希望文章能够帮你解决ios – 是否可以使用swift将Ipod库中的音乐保存到我的应用程序中?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)