转自:http://www.devdiv.com/forum.php?mod=viewthread&tid=46803&extra=page%3D1%26filter%3Ddigest%26digest%3D1%26digest%3D1

  1. NSArray *dirs = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  2.         NSString *documentsDirectoryPath = [dirs objectAtIndex:0];
  3.         NSString *filePath1 = [[documentsDirectoryPath stringByAppendingPathComponent:@"exported.caf"] retain];
  4.         NSString *filePath2 = [[documentsDirectoryPath stringByAppendingPathComponent:@"exported.caf"] retain];
  5.         NSString *filePath3 = [[documentsDirectoryPath stringByAppendingPathComponent:@"exported.caf"] retain];
  6.         NSString *filePath4 = [[documentsDirectoryPath stringByAppendingPathComponent:@"exported.caf"] retain];
  7.         NSString *filePath5 = [[documentsDirectoryPath stringByAppendingPathComponent:@"exported.caf"] retain];
  8.         NSString *filePath6 = [[documentsDirectoryPath stringByAppendingPathComponent:@"b.caf"] retain];
  9.         AVMutableComposition* composition = [AVMutableComposition composition];
  10.         AVURLAsset* audioAsset1 = [[AVURLAsset alloc]initWithURL:[NSURL fileURLWithPath:filePath1] options:nil];
  11.         AVURLAsset* audioAsset2 = [[AVURLAsset alloc]initWithURL:[NSURL fileURLWithPath:filePath2] options:nil];
  12.         AVURLAsset* audioAsset3 = [[AVURLAsset alloc]initWithURL:[NSURL fileURLWithPath:filePath3] options:nil];
  13.         AVURLAsset* audioAsset4 = [[AVURLAsset alloc]initWithURL:[NSURL fileURLWithPath:filePath4] options:nil];
  14.         AVURLAsset* audioAsset5 = [[AVURLAsset alloc]initWithURL:[NSURL fileURLWithPath:filePath5] options:nil];
  15.         
  16.         AVMutableCompositionTrack *audioTrack1 = [composition addMutableTrackWithMediaType:AVMediaTypeAudio 
  17.                                                                       preferredTrackID:kCMPersistentTrackID_Invalid];
  18.         
  19.         [audioTrack1 insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioAsset1.duration)
  20.                          ofTrack:[[audioAsset1 tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]
  21.                           atTime:kCMTimeZero
  22.                            error:nil];
  23.         [audioTrack1 insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioAsset2.duration) 
  24.                          ofTrack:[[audioAsset2 tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] 
  25.                           atTime:audioAsset1.duration
  26.                            error:nil];
  27.  
  28.         [audioTrack1 insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioAsset3.duration) 
  29.                          ofTrack:[[audioAsset3 tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] 
  30.                           atTime:audioAsset1.duration
  31.                            error:nil];
  32.  
  33.  
  34.         [audioTrack1 insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioAsset4.duration) 
  35.                          ofTrack:[[audioAsset4 tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] 
  36.                           atTime:audioAsset1.duration
  37.                            error:nil];
  38.  
  39.  
  40.         [audioTrack1 insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioAsset5.duration) 
  41.                          ofTrack:[[audioAsset5 tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] 
  42.                           atTime:audioAsset1.duration
  43.                            error:nil];
  44.         
  45.         [self exportAsset:audioTrack1.asset toFilePath:filePath6];