title: How to store two different audios in same m4a file in iOS?
body: <p>I want to accept two mp3 files. These two mp3 files should be stored in the same m4a file (overlayed?) and when that particular m4a file is played both mp3 should be played simultaneously.</p>
  
  <p>I want to play m4a song and determine the path of m4a song on the <code>UIButton</code> action. The code for <code>UIButton</code> action has been given below.</p>
  
  <p>I have taken two different <code>AVAudioPlayer</code> to accept two mp3s and took a single variable (<code>* audioFileOutput</code>) to store the destination m4a file.</p>
  
  <p>I have imported two libraries in .m file:</p>
  
  <pre><code>#import "ViewController.h"
  #import &lt;AVFoundation/AVFoundation.h&gt; 
  
  -(IBAction)playTheSong
  {    
      // Path of your source audio file
      NSString *strInputFilePath1 = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"saewill.mp3"];
      NSString *strInputFilePath2 = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"kick.mp3"];
      NSURL *audioFileInput1 = [NSURL fileURLWithPath:strInputFilePath1];
      NSURL *audioFileInput2 = [NSURL fileURLWithPath:strInputFilePath2];
  
      // Path of your destination save audio file
      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
      NSString *libraryCachesDirectory = [paths objectAtIndex:0];
  
      NSString *strOutputFilePath = [libraryCachesDirectory stringByAppendingPathComponent:@"output.mov"];
      NSString *requiredOutputPath = [libraryCachesDirectory stringByAppendingPathComponent:@"output.m4a"];
      NSURL *audioFileOutput = [NSURL fileURLWithPath:requiredOutputPath];
  
      [[NSFileManager defaultManager] removeItemAtURL:audioFileOutput error:NULL];
      AVAsset *asset1 = [AVAsset assetWithURL:audioFileInput1];
      AVAsset *asset2 = [AVAsset assetWithURL:audioFileInput2];
  
      AVAssetExportSession *exportSession1 = [AVAssetExportSession exportSessionWithAsset:asset1
                                                                              presetName:AVAssetExportPresetAppleM4A];
      AVAssetExportSession *exportSession2 = [AVAssetExportSession exportSessionWithAsset:asset2
                                                                              presetName:AVAssetExportPresetAppleM4A];
  
      //two store two diifrent mp3 in same variable        
  
      exportSession1.outputURL = audioFileOutput;
      exportSession1.outputFileType = AVFileTypeAppleM4A;
      exportSession2.outputURL = audioFileOutput;
      exportSession2.outputFileType = AVFileTypeAppleM4A;
  
      //initialised AVAudioplayer for 1st mp3.       
      [exportSession1 exportAsynchronouslyWithCompletionHandler:^
       {
           if (exportSession1.status == AVAssetExportSessionStatusCompleted)
           {
               NSLog(@"Success!");
               NSLog(@" OUtput path is \n %@", requiredOutputPath);
               NSFileManager * fm = [[NSFileManager alloc] init];
               [fm moveItemAtPath:strOutputFilePath toPath:requiredOutputPath error:nil];
  
  
               NSURL *url=[NSURL fileURLWithPath:requiredOutputPath];
               NSError *error;
               audioPlayer1=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:&amp;error];
               audioPlayer1.numberOfLoops=0;
               [audioPlayer1 setVolume:0.75];
               [audioPlayer1 play];
  
  
           }
           else if (AVAssetExportSessionStatusFailed == exportSession1.status)
           {
               NSLog(@"failed %@", exportSession1.error.localizedDescription);
           }
       }];
  
      //initialised AVAudioplayer for 2nd mp3.         
  
      [exportSession2 exportAsynchronouslyWithCompletionHandler:^
       {
           if (exportSession2.status == AVAssetExportSessionStatusCompleted)
           {
               NSLog(@"Success!");
               NSLog(@" OUtput path is \n %@", requiredOutputPath);
               NSFileManager * fm = [[NSFileManager alloc] init];
               [fm moveItemAtPath:strOutputFilePath toPath:requiredOutputPath error:nil];
  
  
               NSURL *url=[NSURL fileURLWithPath:requiredOutputPath];
               NSError *error;
               audioPlayer2=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:&amp;error];
               audioPlayer2.numberOfLoops=0;
               [audioPlayer2 setVolume:0.75];
               [audioPlayer2 play];
  
  
           }
           else if (AVAssetExportSessionStatusFailed == exportSession2.status)
           {
               NSLog(@"failed %@", exportSession2.error.localizedDescription);
           }
       }];        
  }
  </code></pre>