-1

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)
    }
}}
Jorge Leitao
  • 19,085
  • 19
  • 85
  • 121

1 Answers1

0

If you want to compress your vide for sharing, you should look into AVAssetExportSession. Change AVAssetExportPresetAppleM4A to AVAssetExportPresetLowQuality . You can do something like this :

- (void)convertVideoToLowQuailtyWithInputURL:(NSURL*)inputURL 
                                   outputURL:(NSURL*)outputURL 
                                     handler:(void (^)(AVAssetExportSession*))handler
{
    [[NSFileManager defaultManager] removeItemAtURL:outputURL error:nil];
    AVURLAsset *asset = [AVURLAsset URLAssetWithURL:inputURL options:nil];
    AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetLowQuality];
    exportSession.outputURL = outputURL;
    exportSession.outputFileType = AVFileTypeQuickTimeMovie;
    [exportSession exportAsynchronouslyWithCompletionHandler:^(void) 
    {
        handler(exportSession);
        [exportSession release];
    }];
}

You can also set AVFormatIDKey to kAudioFormatMPEG4AAC for recordSettings

Munahil
  • 2,381
  • 1
  • 14
  • 24