Skip to content

Instantly share code, notes, and snippets.

@pita5
Created September 3, 2012 14:04
Show Gist options
  • Save pita5/3609535 to your computer and use it in GitHub Desktop.
Save pita5/3609535 to your computer and use it in GitHub Desktop.
- (void)_processAudio:(AVURLAsset *)audioAsset
present:(NSString *)preset
handler:(void(^)(double percentComplete, NSError* error))handler
{
NSString* tempDir = NSTemporaryDirectory();
NSString* exportPath = [tempDir stringByAppendingPathComponent:[NSString randomString]];
if ([[NSFileManager defaultManager] fileExistsAtPath:exportPath])
{
[[NSFileManager defaultManager] removeItemAtPath:exportPath
error:nil];
}
NSURL* exportURL = [NSURL fileURLWithPath:exportPath];
AVAssetExportSession* exportSession = [[AVAssetExportSession alloc] initWithAsset:audioAsset presetName:preset];
exportSession.outputURL = exportURL;
exportSession.shouldOptimizeForNetworkUse = YES;
exportSession.outputFileType = AVFileTypeAppleM4V;
__weak PLGYouTubeUploader* wself = self;
NSTimer* progressTimer = [NSTimer scheduledTimerWithTimeInterval:0.1
block:^{
switch (exportSession.status)
{
case AVAssetExportSessionStatusFailed:
[wself _failAudioProcessing:handler];
break;
case AVAssetExportSessionStatusCancelled:
[wself _failAudioProcessing:handler];
break;
default:
handler(exportSession.progress, nil);
break;
}
}
repeats:YES];
[exportSession exportAsynchronouslyWithCompletionHandler:^{
[progressTimer invalidate];
if (exportSession.status == AVAssetExportSessionStatusCompleted)
{
NSString* path = exportSession.outputURL.path;
}
else
{
SHOW_ALERT_ERROR(@"")
}
}];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment