I am working with accessing Itunes Library music. I stored a selected song in my Document directory. The song's size is 13MB, and I need to reduce the song size so that I can easily send it to the server. How do I to it? Here is my overall code:
@IBAction func AddSongs(sender: UIButton)
{
displayMediaPickerAndPlayItem()
}func displayMediaPickerAndPlayItem()
{mediaPicker = MPMediaPickerController(mediaTypes: .AnyAudio)
if let picker = mediaPicker{
print("Successfully instantiated a media picker")
picker.delegate = self
view.addSubview(picker.view)
presentViewController(picker, animated: true, completion: nil)
} else {
print("Could not instantiate a media picker")
}
}
MPMediaPickerController, didPickMediaItems mediaItemCollection Function
let item: MPMediaItem = mediaItemCollection.items[0]
print(mediaItemCollection)
print("mediaItemCollection = \(mediaItemCollection)")
let url = item.valueForProperty(MPMediaItemPropertyAssetURL) as! NSURL
FinalAudioName = item.valueForProperty(MPMediaItemPropertyTitle) as! NSString as String
// let songAsset = AVURLAsset.init(URL: url, options: nil)
// print("songAsset = \(songAsset)")
export(url, completionHandler: { _, _ in })
func export(assetURL: NSURL, completionHandler: (NSURL?, ErrorType?) -> ()) {
let asset = AVURLAsset(URL: assetURL)
guard let exporter = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetAppleM4A) else {
completionHandler(nil, 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, nil)
} else {
completionHandler(nil, exporter.error)
}
}}