循环AVMutableCompositionTrack [英] Loop AVMutableCompositionTrack

查看:149
本文介绍了循环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];

如您所见,第一个 AVAsset 是第二条轨道的基本长度,这意味着如果我有一条较长的第二条轨道,它将被剪切,这正是我想要的方式.

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.

经过研究,我发现没有一种便捷的方法可以在iOS中实际循环播放曲目.解决方法之一是一遍又一遍地在 AVMutableComposition 中插入第二个音轨,但这对我来说听起来很奇怪.任何基于该主题的想法都将非常有用.

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.

推荐答案

我认为它应该起作用:

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

这篇关于循环AVMutableCompositionTrack的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆