Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><code>Step 1 :</code> Add <code>AVFoundation.Framework</code> and <code>CoreMedia.Framework</code> in your Project</p> <p><code>Step 2 :</code> <code>#import &lt;AVFoundation/AVFoundation.h&gt;</code> in your <code>.h</code> file</p> <p><code>Step 3 :</code> <code>Write following method in your .m file</code> </p> <p><code>Step 4 :</code> <code>Call this function from ViewDidAppear to create Merged Audio file.</code></p> <pre><code>// Get Asset Detail - (AVAsset *)getAvAssetForFile:(NSString *)fileName andType:(NSString *)fileType { NSString *URLPath = [[NSBundle mainBundle] pathForResource:fileName ofType:fileType]; NSURL *assetURL = [NSURL fileURLWithPath:URLPath]; AVAsset *asset = [AVAsset assetWithURL:assetURL]; return asset; } - (void)mergeAudioFiles { // `An Array with List of Audio Clips to be Merged, they could be of different type also` NSMutableArray *listOfAudio = [[NSMutableArray alloc] initWithCapacity:0]; // `Create AVAsset Object from your audio files and add to array` [listOfAudio addObject:[self getAvAssetForFile:@"audioFile1" andType:@"caf"]]; [listOfAudio addObject:[self getAvAssetForFile:@"audioFile2" andType:@"caf"]]; [listOfAudio addObject:[self getAvAssetForFile:@"audioFile3" andType:@"aiff"]]; AVMutableComposition *composition = [AVMutableComposition composition]; CMTime current = kCMTimeZero; NSError *compositionError = nil; for(AVAsset *asset in listOfAudio) { BOOL result = [composition insertTimeRange:CMTimeRangeMake(kCMTimeZero, [asset duration]) ofAsset:asset atTime:current error:&amp;compositionError]; if(!result) { if(compositionError) { // manage the composition error case NSLog(@"Error in Merging : %@",[compositionError debugDescription]); } } else { current = CMTimeAdd(current, [asset duration]); } } AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset: composition presetName: AVAssetExportPresetAppleM4A]; exporter.outputFileType = @"com.apple.m4a-audio"; // Set Output FileName NSString *fileName = @"myAudioTrack"; NSString *exportFile = [DOC_DIR_PATH stringByAppendingFormat: @"/%@.m4a", fileName]; // set up export NSURL *exportURL = [NSURL fileURLWithPath:exportFile]; exporter.outputURL = exportURL; // do the export [exporter exportAsynchronouslyWithCompletionHandler:^{ int exportStatus = exporter.status; switch (exportStatus) { case AVAssetExportSessionStatusFailed: NSLog (@"AVAssetExportSessionStatusFailed: %@", exporter.debugDescription); break; case AVAssetExportSessionStatusCompleted: NSLog (@"AVAssetExportSessionStatusCompleted"); 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; } }]; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload