1

I'm trying to mix two audio files (lay one audio file on top of the other - not stitched together) but I'm struggling with learning AVFoundation on IOS. I've followed this answer here: How to merge Audio and video using AVMutableCompositionTrack

And this is what I have :

//NSURL *audioFilePath = [NSURL fileURLWithPath:@"var/mobile/Applications/822732B6-67B9-485F-BA44-FAACAB34C4FD/Documents/Coisir Cheoil10_09_2014_1429.m4a"];
NSURL *audioUrl = [NSURL fileURLWithPath:@"var/mobile/Applications/822732B6-67B9-485F-BA44-FAACAB34C4FD/Documents/Robot R-3-311_09_2014_2252.m4a"];
// need to fix link to backing Track
NSURL *backingTrackURL = [NSURL fileURLWithPath:@"var/mobile/Applications/822732B6-67B9-485F-BA44-FAACAB34C4FD/Documents/Robot R-3-316_09_2014_1559.m4a"];// need ot fix the link to this
AVURLAsset* backingTrack = [[AVURLAsset alloc] initWithURL:audioUrl options:nil];
AVURLAsset* voiceTrack = [[AVURLAsset alloc] initWithURL:backingTrackURL options:nil];

AVMutableComposition* mixComposition = [AVMutableComposition composition];

AVMutableCompositionTrack *compositionCommentaryTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];


[compositionCommentaryTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, backingTrack.duration)
                                    ofTrack:[[backingTrack tracksWithMediaType:AVMediaTypeAudio]objectAtIndex:0]
                                     atTime:kCMTimeZero
                                      error:nil];

AVMutableCompositionTrack *compositionVoiceTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];

[compositionVoiceTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, voiceTrack.duration)
                               ofTrack:[[voiceTrack tracksWithMediaType:AVMediaTypeAudio]objectAtIndex:0] atTime:kCMTimeZero error:nil];

AVAssetExportSession* _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetHighestQuality];

NSString* mixedAudio = @"mixedAudio.m4a";

NSString *exportPath = [NSTemporaryDirectory() stringByAppendingString:mixedAudio];
NSURL *exportURL = [NSURL fileURLWithPath:exportPath];

if ([[NSFileManager defaultManager]fileExistsAtPath:exportPath]) {
    [[NSFileManager defaultManager]removeItemAtPath:exportPath error:nil];
}
_assetExport.outputFileType = @"m4a";
_assetExport.outputURL = exportURL;
_assetExport.shouldOptimizeForNetworkUse = YES;

[_assetExport exportAsynchronouslyWithCompletionHandler:^{
    NSLog(@"Completed Sucessfully");
}];

The code fails when I try to set the timeranges with an error index 0 beyond bounds for empty array.  I take it that backing track does not include tracksWithMediaTye:AvMediaTypeAudio and that is why that fails.  

I suspect it is because I am loading in .m4a audio files and not video files like the original answer on StackOverflow was intended for. So my question is how do I Mix two seperate audio files and save them as a new combined file. The use is I have a backing track, the user records their own vocals and then can mix their vocals with the backing track and send the mixed audio to themselves.

Thanks for any suggestions. I'm finding AVFoundation quite daunting.

Community
  • 1
  • 1
Linda Keating
  • 2,215
  • 7
  • 31
  • 63
  • What's happening when you run this? Are the 2 audio tracks identical in length? – ChrisH Sep 16 '14 at 22:11
  • What was happening was when I was inserting the Time range it was looking for objectAtIndex:0 of the tracks and was crashing then. I have since re-worked the code (I'll post it below) and it is working. I think the code above wasn't actually loading any files. The filePath I gave must have been wrong. – Linda Keating Sep 16 '14 at 22:15

2 Answers2

6

I'm posting the code that I eventually got to work, in case anybody else is trying to do the same thing and would like some code samples (My problem above I suspect was that the audio files weren't being loaded correctly)

 [self showActivityIndicator]; // This code takes a while so show the user an activity Indicator
AVMutableComposition *composition = [AVMutableComposition composition];
NSArray* tracks = [NSArray arrayWithObjects:@"backingTrack", @"RobotR33", nil];
NSString* audioFileType = @"wav";

for (NSString* trackName in tracks) {
    AVURLAsset* audioAsset = [[AVURLAsset alloc]initWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:trackName ofType:audioFileType]]options:nil];

    AVMutableCompositionTrack* audioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio
                                                                     preferredTrackID:kCMPersistentTrackID_Invalid];

    NSError* error;
    [audioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioAsset.duration) ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio]objectAtIndex:0] atTime:kCMTimeZero error:&error];
    if (error)
    {
        NSLog(@"%@", [error localizedDescription]);
    }
}
AVAssetExportSession* _assetExport = [[AVAssetExportSession alloc] initWithAsset:composition presetName:AVAssetExportPresetAppleM4A];

NSString* mixedAudio = @"mixedAudio.m4a";

NSString *exportPath = [NSTemporaryDirectory() stringByAppendingString:mixedAudio];
NSURL *exportURL = [NSURL fileURLWithPath:exportPath];

if ([[NSFileManager defaultManager]fileExistsAtPath:exportPath]) {
    [[NSFileManager defaultManager]removeItemAtPath:exportPath error:nil];
}
_assetExport.outputFileType = AVFileTypeAppleM4A;
_assetExport.outputURL = exportURL;
_assetExport.shouldOptimizeForNetworkUse = YES;

[_assetExport exportAsynchronouslyWithCompletionHandler:^{
    [self hideActivityIndicator];
    NSLog(@"Completed Sucessfully");
}];
Linda Keating
  • 2,215
  • 7
  • 31
  • 63
  • 1
    Hi,Thanks for your answer..But if I mix my voice recorder .m4a format and karaoke song .mp3 format.It's merge successfully. But if hear the output merged files....My voice is very low and Music is very high. How to increase my voice recorder sound and decrease the song sound ????? –  Dec 10 '16 at 07:41
  • And one more question: After merged both song...I get a large amount of data.How to reduce the size after merging two audios –  Dec 10 '16 at 07:44
3

For Swift 3

    showActivityIndicator()  
    var composition = AVMutableComposition()
    var tracks: [Any] = ["backingTrack", "RobotR33"]
    var audioFileType: String = "wav"
    for trackName: String in tracks {
        var audioAsset = AVURLAsset(url: URL(fileURLWithPath: Bundle.main.path(forResource: trackName, ofType: audioFileType)), options: nil)
        var audioTrack: AVMutableCompositionTrack? = composition.addMutableTrack(withMediaType: AVMediaTypeAudio, preferredTrackID: kCMPersistentTrackID_Invalid)
        var error: Error?
        try? audioTrack?.insertTimeRange(CMTimeRangeMake(kCMTimeZero, audioAsset.duration), of: audioAsset.tracks(withMediaType: AVMediaTypeAudio)[0], atTime: kCMTimeZero)
        if error != nil {
            print("\(error?.localizedDescription)")
        }
    }
    var _assetExport = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetAppleM4A)
//  Converted with Swiftify v1.0.6355 - https://objectivec2swift.com/
var mixedAudio: String = "mixedAudio.m4a"
var exportPath: String = NSTemporaryDirectory() + (mixedAudio)
var exportURL = URL(fileURLWithPath: exportPath)
if FileManager.default.fileExists(atPath: exportPath) {
    try? FileManager.default.removeItem(atPath: exportPath)
}
assetExport.outputFileType = AVFileTypeAppleM4A
assetExport.outputURL = exportURL
assetExport.shouldOptimizeForNetworkUse = true
assetExport.exportAsynchronously(withCompletionHandler: {() -> Void in
    self.hideActivityIndicator()
    print("Completed Sucessfully")
})
BHAVIK
  • 890
  • 7
  • 35