2013-08-07 20 views
2

我試圖將一堆視頻縫合在一起,然後通過iOS中的視頻添加一些音樂。音頻使用AVMutableAudioMix添加。但是,當視頻最終導出時,音頻混合丟失。下面是代碼的樣子:使用AVFoundation在iOS中混音不起作用

- (void)mergeVideos:(NSArray *)videos{ 
    AVMutableComposition *mixComposition = [[AVMutableComposition alloc] init]; 
    AVMutableCompositionTrack *videoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo 
                     preferredTrackID:kCMPersistentTrackID_Invalid]; 
    AVMutableCompositionTrack *audioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio 
                     preferredTrackID:kCMPersistentTrackID_Invalid]; 
    CMTime currentTime = kCMTimeZero; 
    for (AVAsset *asset in videos) { 
     // 2 - Video track 
     [videoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, asset.duration) 
          ofTrack:[[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:currentTime error:nil]; 
     [audioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, asset.duration) 
          ofTrack:[[asset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:currentTime error:nil]; 
     currentTime = CMTimeAdd(currentTime, asset.duration); 
    } 
     // 4 - Get path 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *myPathDocs = [documentsDirectory stringByAppendingPathComponent: 
          [NSString stringWithFormat:@"mergeVideo-%d.mp4",arc4random() % 1000]]; 
    NSURL *url = [NSURL fileURLWithPath:myPathDocs]; 
    // 5 - Create exporter 
    NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:mixComposition]; 
    NSString *quality = AVAssetExportPresetHighestQuality; 
    //load the audio 
    AVMutableAudioMix *audioMix = nil; 
    NSURL *audioURL = [self loadAudioFile];// gives a url for a .caf file from the bundle 
    if (audioURL) { 
     AVURLAsset *audioAsset = [AVURLAsset assetWithURL:audioURL]; 
     AVAssetTrack *aTrack = (AVAssetTrack *)[[audioAsset tracksWithMediaType:AVMediaTypeAudio] firstObject]; 

     AVMutableAudioMixInputParameters *trackMix = 
     [AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:aTrack]; 

     [trackMix setVolumeRampFromStartVolume:0 toEndVolume:1 timeRange: 
     CMTimeRangeMake(CMTimeMakeWithSeconds(0, 1), CMTimeMakeWithSeconds(3, 1))]; 
     [trackMix setVolume:1.0 atTime:kCMTimeZero]; 
     AVMutableAudioMix *audioMix = [AVMutableAudioMix audioMix]; 
     audioMix.inputParameters = [NSArray arrayWithObject:trackMix]; 
    } 

    AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:mixComposition 
                     presetName:quality]; 
    if (audioMix) { 
     exporter.audioMix = audioMix; 
    } 
    exporter.outputURL=url; 
    exporter.outputFileType = AVFileTypeMPEG4; 
    exporter.shouldOptimizeForNetworkUse = YES; 
    [exporter exportAsynchronouslyWithCompletionHandler:^{ 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      [self exportDidFinish:exporter]; 
     }); 
    }]; 
} 

執行時沒有錯誤。只要.caf文件中包含的音樂不會混合到導出的文件中。任何想法是怎麼回事?

回答

3

您尚未將.caf文件中的音頻插入任何AVMutableCompositionTrack,因此與aTrack關聯的音頻混合不會調整.caf文件的音量。如果您想從文件的.caf音頻被列入與相關的音頻混合視頻,創建另一個AVMutableCompositionTrack持有從文件的.caf音頻:

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

(與時間範圍設置根據自己的喜好):

[audioTrackCAF insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioAsset.duration) 
          ofTrack:aTrack atTime:kCMTimeZero error:nil] 

此外,有利於傳遞(的NSError *代替nil),以insertTimeRange:ofTrack:atTime:error,以確保你有有效時間範圍和插入您的媒體。

相關問題