16

I am trying to do that using AVAsset to record Audio file and then after first i store it on NSFileManager and after that by convert it to nsdata i Call API to store it.

I am successful to Create AVAsset recording file and play it using third party Class that is SCPlayer.

Now problem is that i don't know how to Use AVAsset file for save it in file manager and then after call API to sent it by converting it to NSData.

Is any way to convert AVAsset to NSData???

Please Help...

narner
  • 2,908
  • 3
  • 26
  • 63
Dhaval Tannarana
  • 181
  • 1
  • 2
  • 9
  • I posted a solution to this problem here: http://stackoverflow.com/questions/37611488/how-to-stream-a-video-with-avurlasset-and-save-to-disk-the-cached-data/37611489#37611489 For me it was not working only with AVAssetExportSession. I also added AVAssetResourceLoaderDelegate to make it work. – Gabriel.Massana Jun 03 '16 at 10:09

3 Answers3

25

You can do the following:

  1. Use AVAssetExportSession to export your AVAsset object to a file path URL.
  2. Convert it to NSData using its dataWithContentsOfURL method.

    
    NSURL *fileURL = nil;
    __block NSData *assetData = nil;
    
    // asset is you AVAsset object
    AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetHighestQuality];
    
    exportSession.outputURL = fileURL;
    // e.g .mov type 
    exportSession.outputFileType = AVFileTypeQuickTimeMovie; 
    
    [exportSession exportAsynchronouslyWithCompletionHandler:^{
        assetData = [NSData dataWithContentsOfURL:fileURL];
        NSLog(@"AVAsset saved to NSData.");
    }];
    
  3. Don't forget to clean up the output file after doing whatever you need to do with it ;)

Jorge
  • 1,066
  • 8
  • 16
  • I think it is the way, since initializing NSData from library URL is not accepted (returns nil NSData) – onmyway133 Sep 08 '16 at 07:55
  • 1
    Is there any way to export to an NSData variable in memory instead of directly saving to the file system? – Ryn9011 Sep 29 '20 at 23:31
7

Swift 3

let manager = PHImageManager.default()    
manager.requestAVAsset(forVideo: asset, options: nil, resultHandler: { (avasset, audio, info) in
                if let avassetURL = avasset as? AVURLAsset {
                    guard let video = try? Data(contentsOf: avassetURL.url) else {
                        return
                    }
                    videoData = video
                }
            })
dungi
  • 302
  • 3
  • 7
3

You could have a look at AVAssetExportSession

AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetHighestQuality];
exporter.outputURL = exportUrl;

[exporter exportAsynchronouslyWithCompletionHandler:^{
    // here your file will be saved into file system at specified exportUrl
}];
Ivan Golub
  • 89
  • 8
  • 1
    This worked for me, but I needed to add the following line: ` exporter.outputFileType = AVFileTypeMPEG4;` because outputFileType cannot be NULL – cph2117 Aug 20 '15 at 17:48