在混合使用IOS AVComposition两个音频文件 [英] Mixing two Audio Files using AVComposition on IOS

查看:525
本文介绍了在混合使用IOS AVComposition两个音频文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图混合两种音频文件(躺在另一个的上面一个音频文件 - 不缝合在一起),但我对IOS的学习AVFoundation挣扎。我跟着在这里这样的回答:<一href=\"http://stackoverflow.com/questions/15150578/how-to-merge-audio-and-video-using-avmutablecompositiontrack\">How合并音频和视频使用AVMutableCompositionTrack

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

这是我有:

//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.  

我怀疑这是因为我在加载的.m4a音频文件,而不是像计算器原创答案视频文件的目的是为。所以我的问题是我怎么混两个单独的音频文件并将其保存为一个新的联合文件。使用就是我有一个依托轨道,用户录制自己的歌声,然后可以使用背景音轨混合他们的歌声和发送混合音频自己。

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.

感谢您的任何建议。我发现AVFoundation相当艰巨。

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

推荐答案

我张贴了code,我终于开始工作,以防其他​​人试图做同样的事情,想一些code样品(我的问题上面我怀疑是音频文件没有被正确加载)

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");
}];

这篇关于在混合使用IOS AVComposition两个音频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆