2012-09-27 55 views
5

所以我有两个音频文件,相同的格式,可能不同的长度。我想合并这些文件(将音频从一个音频叠加到另一个上,而不是在末尾加入)。如何在Xcode中重叠音频文件并合并iPhone?

可以说我有两个文件:

  1. 音频文件,长达30秒,大小220K
  2. 音频文件B,长度为45秒,大小300K

我会像一个合并音频文件:

  1. 音频文件C,长度45秒,大小300k(我承认这可能更多)

感谢大家的帮助!

+0

嗨@Beat,我有问题,音频文件C的文件大小非常大。你也有这个问题吗?如果是的话,你如何解决它? thx – Newbie

回答

4

这是我在我的应用程序中所做的。

- (void) setUpAndAddAudioAtPath:(NSURL*)assetURL toComposition:(AVMutableComposition *)composition 
{ 
    AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL:assetURL options:nil]; 

    AVMutableCompositionTrack *track = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid]; 
    AVAssetTrack *sourceAudioTrack = [[songAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]; 

    NSError *error = nil; 
    BOOL ok = NO; 

    CMTime startTime = CMTimeMakeWithSeconds(0, 1); 
    CMTime trackDuration = songAsset.duration; 
    //CMTime longestTime = CMTimeMake(848896, 44100); //(19.24 seconds) 
    CMTimeRange tRange = CMTimeRangeMake(startTime, trackDuration); 

    //Set Volume 
    AVMutableAudioMixInputParameters *trackMix = [AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:track]; 
    [trackMix setVolume:0.8f atTime:startTime]; 
    [self.audioMixParams addObject:trackMix]; 

    //Insert audio into track 
    ok = [track insertTimeRange:tRange ofTrack:sourceAudioTrack atTime:CMTimeMake(0, 44100) error:&error]; 
} 


- (IBAction)saveRecording 
{ 
    AVMutableComposition *composition = [AVMutableComposition composition]; 
     audioMixParams = [[NSMutableArray alloc] initWithObjects:nil]; 

    //IMPLEMENT FOLLOWING CODE WHEN WANT TO MERGE ANOTHER AUDIO FILE 
    //Add Audio Tracks to Composition 
    NSString *URLPath1 = [[NSBundle mainBundle] pathForResource:@"mysound" ofType:@"mp3"]; 
    NSString *URLPath2 = [[NSBundle mainBundle] pathForResource:@"mysound2" ofType:@"mp3"]; 
    NSURL *assetURL1 = [NSURL fileURLWithPath:URLPath1]; 
    [self setUpAndAddAudioAtPath:assetURL1 toComposition:composition]; 

    NSURL *assetURL2 = [NSURL fileURLWithPath:URLPath2]; 
    [self setUpAndAddAudioAtPath:assetURL2 toComposition:composition]; 

    AVMutableAudioMix *audioMix = [AVMutableAudioMix audioMix]; 
    audioMix.inputParameters = [NSArray arrayWithArray:audioMixParams]; 

    //If you need to query what formats you can export to, here's a way to find out 
    NSLog (@"compatible presets for songAsset: %@", 
      [AVAssetExportSession exportPresetsCompatibleWithAsset:composition]); 

    AVAssetExportSession *exporter = [[AVAssetExportSession alloc] 
             initWithAsset: composition 
             presetName: AVAssetExportPresetAppleM4A]; 
    exporter.audioMix = audioMix; 
    exporter.outputFileType = @"com.apple.m4a-audio"; 
    NSURL *exportURL = [NSURL fileURLWithPath:exportFile]; 
    exporter.outputURL = exportURL; 

// do the export 
     [exporter exportAsynchronouslyWithCompletionHandler:^{ 
      int exportStatus = exporter.status; 
      NSError *exportError = exporter.error; 

      switch (exportStatus) { 
       case AVAssetExportSessionStatusFailed: 

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

要注意的是我没有做到这一点前一阵子,你可能有它的工作一点点,使其工作。但它确实有用。让我知道你是否有问题。

+0

hi @ gg13,我用你建议的方式混音。但我的问题是导出的文件比原来的文件太大。有没有办法以较小的文件类型渲染文件? – Newbie

+0

我试图将exporter.outputFileType更改为其他文件格式,如AVFileTypeAIFC。但是,它会引发错误。然后我读取导出器的supportedFileTypes属性,我发现它只支持AVFileTypeAppleM4A(即“com.apple.m4a-audio”是您建议的文件类型)。这是为什么? – Newbie

+0

我看到您将trackmix的音量设置为0.8。这是避免破坏质量的唯一必要步骤吗?我对音频混音知之甚少,并且在合并文件时收到了不好的结果,所以我会尝试一下,但是您知道它是否有助于增加比特率吗?比如,如果我将两个音频文件合并为44.1,如果合并文件为88.2(或更常见的96),结果会更好,还是没有理由这样做? – Sti

0

如果在所选资产中找不到音频资产轨道,您可以使用此语句来检查特定视频的声音。

if([[songAsset tracksWithMediaType:AVMediaTypeAudio] firstObject]==NULL) 
{ 
    NSLog(@"Sound is not Present"); 
} 
else 
{ 
    NSLog(@"Sound is Present"); 
    //You will initalise all things 
    AVAssetTrack *sourceAudioTrack = [[songAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]; 

}