0

我想知道如何将给定的音频文件与两个视频文件结合起来。在合并音频时我被卡住了,但是视频很好,这是我到目前为止所做的

- (IBAction)MergeAndSave:(id)sender{
    if(firstAsset !=nil && secondAsset!=nil){
        [ActivityView startAnimating];
        //Create AVMutableComposition Object.This object will hold our multiple AVMutableCompositionTrack.
        AVMutableComposition* mixComposition = [[AVMutableComposition alloc] init];

        //VIDEO TRACK
        AVMutableCompositionTrack *firstTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
        [firstTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, firstAsset.duration) ofTrack:[[firstAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:kCMTimeZero error:nil];

        AVMutableCompositionTrack *secondTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
        [secondTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, secondAsset.duration) ofTrack:[[secondAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:firstAsset.duration error:nil];

        //AUDIO TRACK
        if(audioAsset==nil){

            NSURL* musicFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                                       pathForResource:@"click"
                                                       ofType:@"caf"]];

            AVMutableCompositionTrack *click = [[AVMediaTypeAudio] initWithContentsOfURL:musicFile error:nil];
            [click insertTimeRange:CMTimeRangeMake(kCMTimeZero, CMTimeAdd(firstAsset.duration, secondAsset.duration))
                                ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:kCMTimeZero error:nil];


                   }

如您所见,我的音频代码根本不起作用

4

1 回答 1

1

将 audioAsset 的类型更改为 AVURLAsset 而不是 AVAsset ...希望它有效

如果不试试这个...如何在 Xcode 中重叠音频文件并为 iPhone 组合?

于 2013-03-20T09:07:21.473 回答