Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- NSURL *assetURL = [NSURL URLWithString:_assertURL];
- AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL:assetURL options:nil];
- /* approach 1: export just the song itself
- */
- AVAssetExportSession *exporter = [[AVAssetExportSession alloc]
- initWithAsset: songAsset
- presetName: AVAssetExportPresetAppleM4A];
- NSLog (@"created exporter. supportedFileTypes: %@", exporter.supportedFileTypes);
- exporter.outputFileType = @"com.apple.m4a-audio";
- NSString *exportFile = [myDocumentsM4aDirectory() stringByAppendingPathComponent: [NSString stringWithFormat:@"%@.m4a",_title]];
- // end of approach 1
- // set up export (hang on to exportURL so convert to PCM can find it)
- [self myDeleteFile:exportFile];
- //[exportURL release];
- _exportURL = [NSURL fileURLWithPath:exportFile];
- exporter.outputURL = _exportURL;
- // do the export
- [exporter exportAsynchronouslyWithCompletionHandler:^{
- int exportStatus = exporter.status;
- switch (exportStatus) {
- case AVAssetExportSessionStatusFailed: {
- // log error to view
- NSError *exportError = exporter.error;
- NSLog (@"AVAssetExportSessionStatusFailed: %@", exportError);
- break;
- }
- case AVAssetExportSessionStatusCompleted: {
- NSLog (@"AVAssetExportSessionStatusCompleted");
- //Final document url
- NSURL *audioUrl = _exportURL;
- NSLog(@"Audio Url=%@",audioUrl);
- break;
- }
- case AVAssetExportSessionStatusUnknown: {
- NSLog (@"AVAssetExportSessionStatusUnknown");
- break;
- }
- case AVAssetExportSessionStatusExporting: {
- NSLog (@"AVAssetExportSessionStatusExporting");
- break;
- }
- case AVAssetExportSessionStatusCancelled: {
- NSLog (@"AVAssetExportSessionStatusCancelled");
- break;
- }
- case AVAssetExportSessionStatusWaiting: {
- NSLog (@"AVAssetExportSessionStatusWaiting");
- break;
- }
- default: {
- NSLog (@"didn't get export status");
- break;
- }
- }
- }];
- }
- - (void)myDeleteFile:(NSString*)path{
- if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
- NSError *deleteErr = nil;
- [[NSFileManager defaultManager] removeItemAtPath:path error:&deleteErr];
- if (deleteErr) {
- NSLog (@"Can't delete %@: %@", path, deleteErr);
- }
- }
- }
- NSString* myDocumentsM4aDirectory(){
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSError *error = nil;
- NSString *dataPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"/m4aFolder"];
- if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
- [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error];
- //Create folde
- return dataPath;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement