0

I am wondering how I would combine a given audio file with two video files. I am stuck when it comes to combining the audio but the video is fine, here is what I have done so far

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


                   }

as you can see my code for the audio doesn't work at all

Kindle Q
  • 944
  • 2
  • 19
  • 28
Lee
  • 23
  • 1
  • 6
  • I've seen a tutorial of Raywenderlich which mergétwo videôandnyou can put a audiofile... Did you seen it? It may help you. – Larme Feb 23 '13 at 23:02
  • @Larme yes i have seen it :) but i want to use a dedicated audio file and not one from the ipod library – Lee Feb 24 '13 at 21:05
  • I guess that the difference is just the music you load: how to get it. – Larme Feb 24 '13 at 21:30
  • yes , but i have tried different code and havent been able to come up with a solution so i turned to you guy hoping yous could help – Lee Feb 24 '13 at 22:03

1 Answers1

1

Change Type of audioAsset to AVURLAsset instead of AVAsset ... hope it works

if not try this... How can I overlap audio files and combine for iPhone in Xcode?

Community
  • 1
  • 1
K'am
  • 129
  • 1
  • 7
  • i solved it AVMutableCompositionTrack *AudioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid]; AVURLAsset *mixAsset = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"file" ofType:@"wav"]] options:nil]; [AudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, CMTimeAdd(firstAsset.duration, secondAsset.duration)) ofTrack:[[mixAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:audioAsset.duration error:nil]; – Lee Mar 21 '13 at 21:46
  • changing the type had nothing to do with making the code work , the solution was the URLAssetWithURL and tracksWithMediaType:AVMediaTypeAudio – Lee Mar 22 '13 at 13:41