Search code examples
ioscocoa-touchcore-audioavmutablecompositionavasset

Loop AVMutableCompositionTrack


I have got two audio tracks on me that I combine with one another like this:

AVMutableComposition *composition = [[AVMutableComposition alloc] init];

AVMutableCompositionTrack *compositionAudioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
[compositionAudioTrack setPreferredVolume:1.0];
AVAsset *avAsset = [AVURLAsset URLAssetWithURL:originalContentURL options:nil];
AVAssetTrack *clipAudioTrack = [[avAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
[compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, avAsset.duration) ofTrack:clipAudioTrack atTime:kCMTimeZero error:nil];

AVMutableCompositionTrack *compositionAudioTrack1 = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
[compositionAudioTrack1 setPreferredVolume:0.01];
NSString *soundOne1  =[[NSBundle mainBundle]pathForResource:@"jingle1" ofType:@"m4a"];
NSURL *url1 = [NSURL fileURLWithPath:soundOne1];
AVAsset *avAsset1 = [AVURLAsset URLAssetWithURL:url1 options:nil];
AVAssetTrack *clipAudioTrack1 = [[avAsset1 tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
[compositionAudioTrack1 insertTimeRange:CMTimeRangeMake(kCMTimeZero, avAsset.duration) ofTrack:clipAudioTrack1 atTime:kCMTimeZero error:nil];

As you can see, the first AVAsset is the base length for the second track, meaning that if I have a long second track it will be cut, which is just the way that I want.

However, I need to be able to loop the second track so that if the first one is too long, the second one goes on and on. I later have to save the resulting track on disc which is an important factor as well.

After a research I've made I found out there's no convenient way to actually loop a track in iOS. One of the ways out would be inserting the second track at AVMutableComposition multiple times over and over but that sounds quite strange to me. Any ideas based on the topic would be really useful.


Solution

  • I think it should work:

    CMTime videoDuration = avAsset.duration;
    if(CMTimeCompare(videoDuration, audioAsset.duration) == -1){
        [compositionAudioTrack1 insertTimeRange:CMTimeRangeMake(kCMTimeZero, avAsset.duration) ofTrack:clipAudioTrack1 atTime:kCMTimeZero error:nil];
    }else if(CMTimeCompare(videoDuration, audioAsset.duration) == 1){
         CMTime currentTime = kCMTimeZero;
         while(YES){
               CMTime audioDuration = audioAsset.duration;
               CMTime totalDuration = CMTimeAdd(currentTime,audioDuration);
               if(CMTimeCompare(totalDuration, videoDuration)==1){
                  audioDuration = CMTimeSubtract(totalDuration,videoDuration);
    
               }
               [compositionAudioTrack1 insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioDuration) ofTrack:clipAudioTrack1 atTime:currentTime error:nil];
               currentTime = CMTimeAdd(currentTime, audioDuration);
               if(CMTimeCompare(currentTime, videoDuration) == 1 || CMTimeCompare(currentTime, videoDuration) == 0){
                   break;
               }
         }
    }