I tried and successfully exported the ipod library song. Please find my code below:
extension AudioPostViewController: MPMediaPickerControllerDelegate { func mediaPicker(mediaPicker: MPMediaPickerController, didPickMediaItems mediaItemCollection: MPMediaItemCollection) { dismissViewControllerAnimated(true, completion: { print("You selected \(mediaItemCollection)") let item: MPMediaItem = mediaItemCollection.items[0] let pathURL: NSURL? = item.valueForProperty(MPMediaItemPropertyAssetURL) as? NSURL if pathURL == nil { Alert.showPopupWithMessage("Unable to read DRM protected file.") return } let title = item.valueForProperty(MPMediaItemPropertyTitle) as? String ?? "Now Playing..." print("\(pathURL), title : \(title) ") // get file extension andmime type let str = pathURL!.absoluteString let str2 = str.stringByReplacingOccurrencesOfString("ipod-library://item/item", withString: "") let arr = str2.componentsSeparatedByString("?") var mimeType = arr[0] mimeType = mimeType.stringByReplacingOccurrencesOfString(".", withString: "") // Export the ipod library as .m4a file to local directory for remote upload let exportSession = AVAssetExportSession(asset: AVAsset(URL: pathURL!), presetName: AVAssetExportPresetAppleM4A) exportSession?.shouldOptimizeForNetworkUse = true exportSession?.outputFileType = AVFileTypeAppleM4A FilePath.removeAudioFile() // Remove file if it exists let fileUrl = FilePath.testFilePathURL() exportSession?.outputURL = fileUrl exportSession?.exportAsynchronouslyWithCompletionHandler({ () -> Void in if exportSession!.status == AVAssetExportSessionStatus.Completed { dispatch_async(dispatch_get_main_queue(), { // Prepare audio to play self.audioTitle = title self.audioPath = fileUrl self.mimeType = "audio/m4a" self.audioFileName = "audio.m4a" UIView.animateWithDuration(0.45, animations: { if Platform.DeviceType.iPhone4 { self.libraryBottomSpacing.constant = 5 self.view.layoutIfNeeded() } else { self.libraryBottomSpacing.constant = 25 self.view.layoutIfNeeded() } }, completion: { (v: Bool) in self.loadJukeBox(self.audioPath!, title: title) }) }) } else { dispatch_async(dispatch_get_main_queue(), { Alert.showPopupWithMessage("Unable to read file as its DRM protected.") }) } }) }) } func mediaPickerDidCancel(mediaPicker: MPMediaPickerController) { dismissViewControllerAnimated(true, completion: nil) } }
Abdul yasin
source share