Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. {
  2. NSURL *assetURL = [NSURL URLWithString:_assertURL];
  3. AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL:assetURL options:nil];
  4.  
  5. /* approach 1: export just the song itself
  6. */
  7. AVAssetExportSession *exporter = [[AVAssetExportSession alloc]
  8. initWithAsset: songAsset
  9. presetName: AVAssetExportPresetAppleM4A];
  10. NSLog (@"created exporter. supportedFileTypes: %@", exporter.supportedFileTypes);
  11. exporter.outputFileType = @"com.apple.m4a-audio";
  12. NSString *exportFile = [myDocumentsM4aDirectory() stringByAppendingPathComponent: [NSString stringWithFormat:@"%@.m4a",_title]];
  13. // end of approach 1
  14.  
  15. // set up export (hang on to exportURL so convert to PCM can find it)
  16. [self myDeleteFile:exportFile];
  17. //[exportURL release];
  18. _exportURL = [NSURL fileURLWithPath:exportFile];
  19. exporter.outputURL = _exportURL;
  20.  
  21. // do the export
  22. [exporter exportAsynchronouslyWithCompletionHandler:^{
  23. int exportStatus = exporter.status;
  24. switch (exportStatus) {
  25. case AVAssetExportSessionStatusFailed: {
  26. // log error to view
  27. NSError *exportError = exporter.error;
  28. NSLog (@"AVAssetExportSessionStatusFailed: %@", exportError);
  29.  
  30. break;
  31. }
  32. case AVAssetExportSessionStatusCompleted: {
  33. NSLog (@"AVAssetExportSessionStatusCompleted");
  34.  
  35. //Final document url
  36.  
  37. NSURL *audioUrl = _exportURL;
  38. NSLog(@"Audio Url=%@",audioUrl);
  39.  
  40.  
  41. break;
  42. }
  43. case AVAssetExportSessionStatusUnknown: {
  44. NSLog (@"AVAssetExportSessionStatusUnknown");
  45.  
  46.  
  47. break;
  48. }
  49. case AVAssetExportSessionStatusExporting: {
  50. NSLog (@"AVAssetExportSessionStatusExporting");
  51.  
  52.  
  53. break;
  54. }
  55. case AVAssetExportSessionStatusCancelled: {
  56. NSLog (@"AVAssetExportSessionStatusCancelled");
  57.  
  58.  
  59. break;
  60. }
  61. case AVAssetExportSessionStatusWaiting: {
  62. NSLog (@"AVAssetExportSessionStatusWaiting");
  63.  
  64. break;
  65. }
  66. default: {
  67. NSLog (@"didn't get export status");
  68.  
  69.  
  70. break;
  71. }
  72. }
  73. }];
  74.  
  75. }
  76.  
  77. - (void)myDeleteFile:(NSString*)path{
  78.  
  79. if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
  80. NSError *deleteErr = nil;
  81. [[NSFileManager defaultManager] removeItemAtPath:path error:&deleteErr];
  82. if (deleteErr) {
  83. NSLog (@"Can't delete %@: %@", path, deleteErr);
  84. }
  85. }
  86.  
  87. }
  88.  
  89. NSString* myDocumentsM4aDirectory(){
  90.  
  91. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  92.  
  93. NSError *error = nil;
  94. NSString *dataPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"/m4aFolder"];
  95.  
  96. if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
  97. [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error];
  98. //Create folde
  99. return dataPath;
  100.  
  101.  
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement