1
votes

Here is part of composition creation:

  AVMutableComposition* mixComposition = [[AVMutableComposition alloc] init];

  for (NSDictionary *track in tracks) {
    NSURL *url = [[NSURL alloc] initWithString:track[@"url"]];
    AVURLAsset *urlAsset = [AVURLAsset URLAssetWithURL:url options:nil];
    AVMutableCompositionTrack *firstTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio
                                                                        preferredTrackID:kCMPersistentTrackID_Invalid];

    [firstTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, urlAsset.duration)
                        ofTrack:[[urlAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]
                         atTime:kCMTimeZero
                          error:nil];
  }
  [self persist:mixComposition for:songId];

Then i wish to persist collection in directory so i do not have to download it each time

Output of composition looks like this:

"AVMutableCompositionTrack: 0x1c4a276a0 trackID = 1, mediaType = soun, editCount = 1", "AVMutableCompositionTrack: 0x1c4a28560 trackID = 2, mediaType = soun, editCount = 1"...,

- (void)persist:(AVMutableComposition *) composition
            for:(NSString *) songId {

  NSLog(@"%@", composition);


  AVAssetExportSession *exportSession = [[AVAssetExportSession alloc]
                                         initWithAsset:composition
                                         presetName:AVAssetExportPresetAppleM4A];
  NSString *path = [NSHomeDirectory() stringByAppendingPathComponent: songId];
  NSURL *url = [NSURL fileURLWithPath: path];
  exportSession.outputURL = url;
  exportSession.shouldOptimizeForNetworkUse = YES;
  exportSession.outputFileType = AVFileTypeAppleM4A;


  //   perform the export
  [exportSession exportAsynchronouslyWithCompletionHandler:^{

    if (AVAssetExportSessionStatusCompleted == exportSession.status) {
      NSLog(@"AVAssetExportSessionStatusCompleted");
      NSLog(@"Path : %@", url);
    } else if (AVAssetExportSessionStatusFailed == exportSession.status) {
      // a failure may happen because of an event out of your control
      // for example, an interruption like a phone call comming in
      // make sure and handle this case appropriately
      NSLog(@"%@", exportSession.error);

    } else {
      NSLog(@"Export Session Status: %ld", (long)exportSession.status);
    }
  }];

}

The error i get:

Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo={NSLocalizedFailureReason=An unknown error occurred (-12780), NSLocalizedDescription=The operation could not be completed, NSUnderlyingError=0x1c0a409f0 {Error Domain=NSOSStatusErrorDomain Code=-12780 "(null)"}}

1

1 Answers

0
votes

the path you use must content type of your file need to export, it's look like:

    let directory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first
    let audioTypeOutput = ".m4a"//type of your file, mp3, m4a for audio
    let exportPath = directory?.appendingPathComponent(name + audioTypeOutput)

hope this help!